Haskell, Simple Continuation -


i having hard time convert simple cps function

this cps style square function

-- : http://en.wikibooks.org/wiki/haskell/continuation_passing_style square :: int -> int square x = x * x  square_cps :: int -> ((int -> r) -> r) square_cps = \cont -> cont (square x) -- square_cps 3 print write '9' out in console 

now, change function arguments in reverse order

square_cps' :: ((int -> r) -> r) -> int square_cps' = ? 

is impossible?

first minor correction definition of square_cps:

square_cps :: int -> ((int -> r) -> r) square_cps x = \cont -> cont (square x)           ^^^ 

alternatively can write:

square_cps x cont = cont (square x) 

note works though type signature makes square_cps function of 1 argument.

now, type signature square_cps' can't work. way written means int out of (int -> r) -> r function returns r.

to flip arguments square_cps, first write equivalent type signature:

square_cps :: int -> (int -> r) -> r               ^      ^             ^--- result               |       \--- second arg               \--- first arg 

and identify arguments shown. swapping first , second arguments results in signature:

square_cps' :: (int -> r) -> int -> r square_cps' cont x = square_cps x cont 

in general, signature a -> b -> c equivalent a -> (b -> c), i.e. function type constructor associates right.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -