[Haskell-cafe] Functions that return functions

michael rice nowgate at yahoo.com
Sun Apr 12 23:50:38 EDT 2009


I'm not sure what you're trying to tell me, so let's try a specific case.


Here's the makeVerifier function that expects its function f to have a single arg, a pair (i,d):

makeVerifier :: ((Int,Int) -> Int) -> Int -> (Int -> Bool)
makeVerifier f m = divides m . foldl (+) 0 . map f . zip [1 .. ] . digits

And usage:
let checkCc = makeVerifier (\ (i d) -> if odd i then d else if d < 5 then 2*d else 2*d + 1) 10



And here's the old makeVerifier function that expects its function f to have two integer arguments, i & d:

makeVerifier :: (Int -> Int -> Int) -> Int -> (Int -> Bool)
makeVerifier f m = divides m . foldl (+) 0 . zipWith f [1 .. ] . digits

And usage:
let checkCc = makeVerifier (\ ....    <== Complete this  ) 10


Michael

--- On Sun, 4/12/09, Ross Mellgren <rmm-haskell at z.odi.ac> wrote:

From: Ross Mellgren <rmm-haskell at z.odi.ac>
Subject: Re: [Haskell-cafe] Functions that return functions
To: "michael rice" <nowgate at yahoo.com>
Cc: "Daniel Fischer" <daniel.is.fischer at web.de>, haskell-cafe at haskell.org
Date: Sunday, April 12, 2009, 9:23 PM

Under the covers of syntax they only have one parameter, but you can write curried lambdas or functions easily:
\ a b -> a + b
which is equivalent to
\ a -> \ b -> a + b
and also equivalent to the "normal" function syntax
f a b = a + b
or
f a = \ b -> a + b
-Ross
On Apr 12, 2009, at 9:09 PM, michael rice wrote:
My question was meant in the context of the makeVerifier function, which is passed a lambda expression. It's my understanding that Haskell lambda expressions can have only a single parameter, which is why I changed the function parameter to a pair, (i,d).

How would it be done otherwise?

Michael


--- On Sun, 4/12/09, Daniel Fischer <daniel.is.fischer at web.de> wrote:

From: Daniel Fischer <daniel.is.fischer at web.de>
Subject: Re: [Haskell-cafe] Functions that return functions
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Sunday, April 12, 2009, 7:20 PM

Am Montag 13 April 2009 01:09:22 schrieb michael rice:
> Example please.
>
> Michael
>

Curried:

f :: a -> b -> c

amenable to partial application.

Uncurried:

g :: (a,b) -> c

not easy to apply partially.

The Prelude contains

curry :: ((a,b) -> c) -> (a -> b -> c)

uncurry :: (a -> b -> c) -> ((a,b) -> c)

to convert if needed.

       _______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090412/79b00816/attachment.htm


More information about the Haskell-Cafe mailing list