|
|
|
|
|
by jabagawee
4390 days ago
|
|
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. |
|
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:
[1]: https://gist.github.com/tfausak/4401ef0b43b5c1db0570 [2]: https://github.com/tfausak/tfausak.github.io/issues/31