haskell - Why does this function work even though the argument is missing? -
i'm trying understand following piece of code:
import data.char (ord) encodeinteger :: string -> integer encodeinteger = read . concatmap ch ch c = show (ord c)
but don't see how can work when encodeinteger
defined function takes string, in second line, function implemented without string argument.
also, concatmap
(according hoogle), takes function , list, function ch
provided.
why code still work? argument somehow magically passed? has currying?
edit: , why doesn't work change this:
encodeinteger :: string -> integer encodeinteger = read . concatmap ch ch c = show (ord c)
basically defining function
f = g
is same defining function
f x = g x
in specific case, can use
encodeinteger = (read . concatmap ch)
to define function. parentheses needed, otherwise parsed as
encodeinteger = (read) . (concatmap ch a)
and concatmap ch a
not function , can not composed. @ write
encodeinteger = read (concatmap ch a) -- or encodeinteger = read $ concatmap ch
about "why concatmap ch
takes 1 argument?". partial application, common in haskell. if have
f x y z = x+y+z
you can call f
fewer arguments, , obtain result function of remaining arguments. e.g., f 1 2
function taking z
, returning 1+2+z
.
concretely, currying, there's no such thing function taking 2 or more arguments. every function takes 1 argument. when have function like
foo :: int -> bool -> string
then foo
takes 1 argument, int
. returns function, takes bool
, returns string
. can visualize writing
foo :: int -> (bool -> string)
anyway, if currying , partial application, find plenty of examples.
Comments
Post a Comment