Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Haskell/Elm talk

Haskell/Elm talk

Lessons learned from using Haskell and Elm to solve a computer game puzzle

Jon Kelly

March 17, 2016
Tweet

More Decks by Jon Kelly

Other Decks in Programming

Transcript

  1. Haskell, Elm – why do they appeal? My motivation –

    Nodejs re-sparked interest in FP Course by RxJs author Still not fluent, looking for chances to try things out HackerRank, noughts and crosses Computer game puzzle first real-world solution using Haskell
  2. Ask questions! How else am I going to learn ?

    Also - it's important to hear what everybody thinks about all this and learn from that Still, is a lot to get through, so instead of one2one chats, there's the pub, email or coffee sometime Really – my contact details are at the end, do get in touch
  3. No problem if you don't get all of it I

    don't . (not kidding here !) There isn't a test at the end (I hope not,anyway) Although all the topics are related, you don't need to grasp all of them to learn the next one. This is really so, it's ok to let stuff go by So whether you ask questions now, later or never, that's fine
  4. Overview Quick intro to Haskell Visual review of solution with

    Elm front-end Go over Haskell code, then talk about solutions Elm - uni-directional design and code review DEMO! Time travel debugging Bonus - solutions in Js/RxJs app & C# Linq (if we get the time)
  5. Don't believe everything you hear! I'm not a Haskell programmer,

    I just write Haskell programs What follows is how I see/understand things It may or may not exactly match the books, wikis, academic papers, mathematical proofs etc.
  6. Haskell type strictness Haskell is relentless in type checking. No

    casts or ignore flags … Can decide not to specify type – Haskell complains if this does not hold up It does have the notion of type groups – num, equality … (C# generic constraints)
  7. Haskell - immutability Nothing changes, we create new things only

    Functions have no side effects - Same input - same output, always except for when we have side effects, but let's move on!
  8. Haskell types The usual Int, Float, String, Bool suspects Lists

    are like arrays Any length – can only hold a single type Tuples are like, er, multiples ? Fixed length – may store multiple types Algebraic create our own from the above (diy) Records object-like syntactic sugar in Elm, a bit duck type-y
  9. Haskell Lists – quick intro Build [1,2,3] like this 1:

    2: 3: [] Pass as params like this xs entire list x:xs x is “head”, xs is “tail” (destructuring) NOTE - access anything past the head – may take a (lot) longer TL;DR – Haskell functions are designed to work with head of list as much as possible
  10. Solution – List operations Wheels represented as lists of Ints

    One list is a Wheel Pos TurnWheel int Wheel Pos drop, take are list operations - ideal for small lists, slower for larger lists
  11. Strange stuff on the way This might all seem a

    bit weird … Feel free to let it go by !
  12. Recursion, pattern matching Pattern matching – a bit like if

    … then … Common Haskell recursion boilerplate (just pseudo code) Fn xs 0 = xs end condition Fn x:xs n = (f x) : Fn xs (n – 1) recurse step e.g. map _ [] = [] map f (x:xs) = f x : map f xs
  13. Solution - Recursion List of Wheel Pos Wheel Loop Not

    a great name, any suggestions? Created by buildWheelLoop Gives us secLoop, thrLoop and ansLoop As shown in app
  14. Solution - Map Given first Wheel, and secLoop Use map

    to go through secLoop and attach to copy of first item This creates a LoopsPermutation - two loops mixed, perhaps In effect, basis for solution/algorithm – see in app Here, Haskell code is little different from any modern language (that uses an anonymous function)
  15. Solution – sumPlusPerms Go through permutations and add anwers to

    each set Add across lists – this is what I wondered about for nought and crosses Here it seems natural enough - for a single column
  16. Solution – findSpecificAnswer Go through permutations and anwers Trawl through

    answers, compare to answers loop Dropwhile is part of a range of elegant functions
  17. Something better Solution works – not sure it scales well

    More on this later Feels big and unwieldy, all those perms
  18. Solution – Second version Wanted a way not to build

    perms items So keep memory use limited to a small amount Since then, have heard about “constant space” Created a revised version of this, which filters the items before processing
  19. Solution – Third version Based on early idea More memory

    per iteration than second But limited to 2 wheel perms So scales infinitely beyond that
  20. Solutions comparison 1 findSpecificAnswer 4,939,444,432 bytes allocated in the heap

    13,881,442,884 bytes copied during GC 1,488,404 bytes maximum residency (9495 sample(s)) 56,556 bytes maximum slop 4 MB total memory in use (0 MB lost due to fragmentation) 25 million items, approx 20 mins
  21. Solutions comparison 2a findSpecificAnswerCS 5,695,287,284 bytes allocated in the heap

    14,737,731,940 bytes copied during GC 1,460,032 bytes maximum residency (10954 sample(s)) 57,016 bytes maximum slop 4 MB total memory in use (0 MB lost due to fragmentation)
  22. Solutions comparison 2b findSpecificAnswerCS 7,630,725,720 bytes allocated in the heap

    19,752,688,420 bytes copied during GC 1,460,060 bytes maximum residency (14680 sample(s)) 54,804 bytes maximum slop 4 MB total memory in use (0 MB lost due to fragmentation) 25 million items, approx 10 mins
  23. Solutions comparison 3 findSpecificAnswerX 2,115,601,208 bytes allocated in the heap

    9,224,696,568 bytes copied during GC 2,454,488 bytes maximum residency (4059 sample(s)) 71,972 bytes maximum slop 6 MB total memory in use (0 MB lost due to fragmentation)
  24. Solutions review 1st – lazy, but possible increases in mem

    usage – however Haskell may treats dropWhile as tail recursion, so may be tight loop 2nd – definitely works in constant space, should be treated as tail recursion 3rd – creates differences between perms – a middle ground in use of memory
  25. Haskell - folds Like loops in other languages Designed to

    be used where lazy eval is, or is not, appropriate Very flexible and powerful Used for main familiar functions, map, filter … And which takes us to Elm
  26. Elm – uni-directional system Model – inputs, data (somewhat merged

    here) View – takes model, creates Html Html events lead to Signal changes (like Redux Actions) Update – takes Signal(s), updates model New model sent to view Considering code as pseudo-code works well
  27. Bonus code - RxJs Uni-directional UI RxJs – deals with

    infinite streams we can choose to get first item only Typescript – preserves Haskell types Do they stand out so clearly Are they useful? What have we gained or lost by this functional programming style ?
  28. Bonus code – C# Linq Not so much done here,

    but get a flavour Like that the Haskell types transfer well We have lazy eval – we declare operations But nothing happens until x amount of data requested What have we gained or lost by this functional programming style ?
  29. Thanks for listening Github: https://github.com/jkbits1 Repo: talkUI (nodejs server for

    apps) HaskellElmTalk (code only) [email protected] Katas and Puzzles https://www.hackerrank.com/ https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems Books: http://learnyouahaskell.com/ http://book.realworldhaskell.org/ https://en.wikibooks.org/wiki/Haskell