I'm new to Haskell and still don't understand how to deal with their type system. My problem is that I'm playing around with the sequenceA function from the book Learn You a Haskell For Great Good. Here's the function:
sequenceA :: (Applicative f) => [f a] -> f [a]
sequenceA = foldr (liftA2 (:)) (pure [])
I tried to adapt it to a specific use, so I wrote the following function:
binner :: Int -> [Int -> Int]
binner n = (map (\x -> bin x) [n, (n-1) .. 1])
where bin n = (`mod` 2) . (`div` 2^(n-1))
I have no problem using these functions separately. For example, the following works great in GHCi:
sequenceA (binner 4) 10
If I type the following in GHCi,
:t (sequenceA (binner 4))
It shows the type as:
(sequenceA (binner 4)) :: Int -> [Int]
However, I can't figure out how to combine the functions. Intuitively, it seems I should be able to do the following, using the same type that GHCi showed:
binner :: Int -> [Int]
binner n = foldr (liftA2 (:)) (pure []) $ (map (\x -> bin x) [n, (n-1) .. 1])
where bin n = (`mod` 2) . (`div` 2^(n-1))
But that throws an error compiling:
Couldn't match type β[a0]β with βIntβ Expected type: [Int] Actual type: [[a0]]
I tried messing with the type declaration, but haven't figured out how to fix it.
Thanks for the help!