Hacker News new | ask | show | jobs
by taylorfausak 4391 days ago
Neat! I also cloned 2048 in Haskell [1]. I don't know how many lines of code it is, but the core game logic is remarkably simple.

[1]: http://taylor.fausak.me/2014/04/28/cloning-2048-in-haskell/

1 comments

I noticed that your blog post differed from the actual code in your git repository. In the blog post, you defined `shift` to be:

   shift v = pad
     (map Just (concatMap add (group (catMaybes v)))))
     Nothing
     (length v)
whereas the code gives:

   shift v = take n (v' <> empty n)
     where
       n = length v
       v' = group (filter isJust v) >>= go
       go (Just a : Just b : ts) = Just (a + b) : go ts
       go ts = ts
From my understanding of the blog post's logic, the former doesn't handle the case where `v = replicate 4 (Just 2)`, since it returns `[Just 4, Just 2, Just 2, Nothing]`. Am I correct, and if so, why does the latter version fix this problem? For reference, I know little to no Haskell.
Wow, excellent catch! I apologize for the discrepancy. I must have made a mistake simplifying the game code for the blog post. I created a Gist [1] to showcase the differences.

The problem in the blog post is the `add` function. It should recurse (like `go` in the Hs2048 package). I fixed the post [2] with this line:

    add (x : y : rest) = x + y : add rest
[1]: https://gist.github.com/tfausak/4401ef0b43b5c1db0570 [2]: https://github.com/tfausak/tfausak.github.io/issues/31
Oh! So `go` is a function that's defined within `shift`. That's the crucial part I was missing. Why is it named "go"?

EDIT: While I have your attention, do you mind also pointing me to some resources to learn about this ">>=" operator? I recall it's something related to monads...

As ibotty said, I named the anonymous inner worker function `go`. From what I can tell, it seems to be pretty common in Haskell.

As for `>>=` (also known as `bind`), I'm using it as a more generic version of `concatMap`. The following expressions are basically the same:

    concat (map (\ x -> [x, -x]) [1, 2, 3])
    concatMap (\ x -> [x, -x]) [1, 2, 3]
    [1, 2, 3] >>= \ x -> [x, -x]
If you want to learn more, check out LYAH [1].

[1]: http://learnyouahaskell.com/a-fistful-of-monads

`go` is a very old name for worker functions in haskell. i have been told it originates in glasgow (where ghc was first developed).
The bind operator (>>=) is commonly expressed in 'do' notation. The following two code snippets are equivalent:

do x <- foo; y <- bar; baz x y

foo >>= \x -> bar >>= \y -> baz x y