I’m new to Haskell. I start solving the Grains problem from Exercism. Here is the implementations of my function.
checkVal :: Integer -> Maybe Integer
checkVal n
| n < 1 || n > 64 = Nothing
| otherwise = Just n
ss :: Integer -> Integer
ss 1 = 1
ss 2 = 2
ss n = case (n - 1) `div` 2 of
0 -> ss (n - 1) * ss (n - 1)
_ -> 2 * ss (n - 1)
Even though ss
has the signature of Integer -> Integer
, I am able to fmap
over it just like this.
ghci> :t (checkVal <$> ss)
(checkVal <$> ss) :: Integer -> Maybe Integer
ghci> :t (fmap checkVal ss)
(fmap checkVal ss) :: Integer -> Maybe Integer
while check the type of fmap checkVal
, I get like this.
ghci> :t (fmap checkVal)
(fmap checkVal) :: Functor f => f Integer -> f (Maybe Integer)
Could anyone explain to me about this?