haskell - Why does the Maybe type wrap its value in a list? -


i'm going through learnyouahaskell, great book, , i'm @ chapter on typeclasses. here's question. in ghci when enter

fmap (++ "string appended to") ("i basic string, not type constructor") 

i error

couldn't match type ‘char’ ‘[char]’ expected type: [[char]]   actual type: [char] in second argument of ‘fmap’, namely   ‘("i basic string, not type constructor")’ 

i understand why i'm getting error (because functor requires type constructor value just "i value being passed maybe type constructor" don't understand why error reads expected type [[char]] maybe type wrapping value in list? what's deal?

list [] functor too. ghc unifies fmap on lists, , deduces first argument type fmap :: (string -> string) -> [string] -> string:

λ prelude > :t (fmap :: (string -> string) -> [string] -> [string]) (++ "string appended to")  ("i basic string, not type constructor")  <interactive>:1:88:     couldn't match type ‘char’ ‘[char]’     expected type: [string]     actual type: [char] 

but expect fmap specialised fmap :: (string -> string) -> maybe string -> maybe string, give different error message:

λ prelude > (fmap :: (string -> string) -> maybe string -> maybe string) (++ "string appended to")  ("i basic string, not type constructor")  <interactive>:11:96:     couldn't match expected type ‘maybe string’                 actual type ‘[char]’     in second argument of ‘fmap ::                                  (string -> string) -> maybe string -> maybe string’, namely       ‘("i basic string, not type constructor")’ 

sometimes ghc doesn't our intentions, , finds "wrong" cause of our error.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -