Haskell parameter is not right? -


i new haskell leaner, code follows:

{-- snippet --} {- | our usual customcolor type play -} data customcolor =   customcolor {red :: int,                green :: int,                blue :: int}   deriving (eq, show, read)  {- | new type stores name , function.  function takes int, applies computation it, , returns int along customcolor -} data funcrec =     funcrec {name :: string,              colorcalc :: int -> (customcolor, int)}  plus5func color x = (color, x + 5)  purple = customcolor 255 0 255  plus5 = funcrec {name = "plus5", colorcalc = plus5func purple } always0 = funcrec {name = "always0", colorcalc = \_ -> (purple, 0)} {-- /snippet --} 

in line "colorcalc=plus5func purple", why there 1 parameter("purple") after plus5func, plus5func should need 2 parameters?

the definition

plus5func color x = (color, x + 5) 

is equivalent to

plus5func color = f     f x = (color, x + 5) 

in other words: plus5func takes 1 argument (color) ad returns function f. returned function takes 1 argument (x) , returns final color&int pair.

this style in "binary" functions represented in haskell , other functional languages. style named "currying" after mathematician/computer scientist "haskell curry".

you can use such function binary functions because

plus5func color x = (plus5func color) x = f x                      {- f above -} = (color, x + 5) 

as bonus "partial application", namely ability of passing fewer arguments (one in case) , getting function of remaning arguments passed (f in case).

so, line

plus5 = funcrec {name = "plus5", colorcalc = plus5func purple } 

actually means

plus5 = funcrec {name = "plus5", colorcalc = f }      f x = plus5func purple x 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -