defining haskell functions that have Maybe in signature -


i'm unclear how write function signatures in haskell, using maybe. consider:

f :: maybe -> maybe f = \a ->  main = print (f (just 5)) 

this works why can't function signature this?

f :: maybe -> maybe  

since f takes maybe type , returns maybe type.

related: if wanted have maybe type more specific , maybe int, why doesn't work?

f :: maybe int -> maybe int f = \a ->  main = print (f (just (int 5))) 

(i'm running code using runhaskell test.hs)

seems confused type variables. first of all, in

f :: maybe -> maybe f = \a -> 

the a's in first line have nothing a's in second line, have written:

f :: maybe -> maybe f = \x -> x 

or even

f :: maybe foo -> maybe foo f = \bar -> bar 

the a's variables stand types. f here declaring f has whole bunch of types @ once:

f :: maybe int -> maybe int f :: maybe string -> maybe string f :: maybe (maybe bool) -> maybe (maybe bool) ... 

and on. not "labeling" of arguments suspect think. fact 2 a's same means argument type has same result type. if had said f :: maybe -> maybe b family:

f :: maybe int -> maybe bool f :: maybe string -> maybe string f :: maybe (maybe bool) -> maybe int ... 

that is, a , b can stand different types, argument , result still have maybe.

the reason can't say

f :: maybe -> maybe 

is because maybe not type -- type constructor. if give type, gives type. maybe int , maybe string types, , in general maybe a type long a type.

maybe int a (which parsed (maybe int) a) has no meaning because maybe int not type constructor -- not accept more arguments.

suggested reading: types , typeclasses lyah.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -