Are haskell type declarations used the same way as python class/function documentation? -


i using "learn haskell tutorial" , have reached type declarations section. understand change way ghci gives error message, affect way actual function works? if not, python function documentation written """ """ underneath "def somefunction(x): "? - example

example code:

removenonuppercase :: [char] -> [char]   removenonuppercase st = [ c | c <- st, c `elem` ['a'..'z']] 

edit: ask because tutorial explains haskell type-inferred @ compile time.

signatures aren't documentation (even though useful well). enforced compiler, means adding signatures can make types of functions more restrictive otherwise. toy example:

add x y = x + y  addint :: int -> int -> int addint x y = x + y 
*main> :t add add :: num => -> -> *main> add 2 3 5 *main> add 2.1 3.1 5.2 *main> :t addint addint :: int -> int -> int *main> addint 2 3 5 *main> addint 2.1 3.1 -- addint not accept non-ints.  <interactive>:23:8:     no instance (fractional int) arising literal ‘2.1’     in first argument of ‘addint’, namely ‘2.1’     in expression: addint 2.1 3.1     in equation ‘it’: = addint 2.1 3.1 

besides that, adding type signatures means better (i.e. easier understand) errors in tricky situations, compiler know want achieve rather having guess on own.

there situations in compiler can't decide types without of signatures or other type annotations. perhaps simplest example is:

readandshow s = show (read s) 

if try use without specifying types...

foo.hs:6:17:     no instance (show a0) arising use of ‘show’     type variable ‘a0’ ambiguous     note: there several potential instances:       instance (ghc.arr.ix a, show a, show b) => show (ghc.arr.array b)         -- defined in ‘ghc.arr’       instance show => show (maybe a) -- defined in ‘ghc.show’       instance (integral a, show a) => show (ghc.real.ratio a)         -- defined in ‘ghc.real’       ...plus 26 others     in expression: show (read s)     in equation ‘readandshow’: readandshow s = show (read s)  foo.hs:6:23:     no instance (read a0) arising use of ‘read’     type variable ‘a0’ ambiguous     note: there several potential instances:       instance (ghc.arr.ix a, read a, read b) => read (ghc.arr.array b)         -- defined in ‘ghc.read’       instance read => read (maybe a) -- defined in ‘ghc.read’       instance (integral a, read a) => read (ghc.real.ratio a)         -- defined in ‘ghc.read’       ...plus 25 others     in first argument of ‘show’, namely ‘(read s)’     in expression: show (read s)     in equation ‘readandshow’: readandshow s = show (read s) failed, modules loaded: none. 

... won't work. read converts string some type, , show opposite. however, if nothing specifies type of read s, compiler can't tell type want read string as. either need specify intermediate type...

readandshowasint s = show (read s :: int) 
*main> readandshowasint "2" "2" 

... or have else pick type you:

readandadd :: string -> int -> int readandadd s y = read s + y 
*main> readandadd "2" 3 5 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -