Re: [Haskell-cafe] Functions that return functions

Thank you. That's just what I needed. And your second version shows that I needn't use an anonymous function (lambda expression) at all, something that got lost in translation from Lisp to Haskell.
First, yes, there was a typo. I couldn't find the original message so had to rewrite.
Second, the reason I switched to a tuple argument in the first place is that I read somewhere, "Real World Haskell" I think, that Haskell anonymous functions could only have a single parameter (\ n -> ...). Maybe I didn't read far enough or Haskell has since been updated.
Then, one poster didn't like the use of a (tuple) argument because it couldn't be partially applied, but the whole point of anonymous functions is for one off stuff, like these verifiers, where one doesn't want to create a named function that's used just once.
In any case, the function we pass to makeVerifier, whether anonymous or in the WHERE of your clarified version, is a function that only appears in the definition of a particular verifier, so what difference can it possibly make whether or not it can be partially applied? That's what I can't get my head around.
Thanks again.
Michael
--- On Sun, 4/12/09, Ross Mellgren
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
This looks like it has a typo -- did you mean \ (i, d) -> rather than \ (i d) -> ?
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
let checkCc = makeVerifier (\ i d -> if odd i then d else if d < 5 then 2*d else 2*d + 1) 10 though I find it a bit nicer to expand it a bit for clarity: checkCc :: Int -> Bool checkCc = makeVerifier f 10 where f i d | odd i = d | d < 5 = 2*d | otherwise = 2*d + 1 -Ross
--- On Sun, 4/12/09, Ross Mellgren
wrote: From: Ross Mellgren
Subject: Re: [Haskell-cafe] Functions that return functions To: "michael rice" Cc: "Daniel Fischer" , haskell-cafe@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
wrote: From: Daniel Fischer
Subject: Re: [Haskell-cafe] Functions that return functions To: "michael rice" Cc: haskell-cafe@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

michael rice
In any case, the function we pass to makeVerifier, whether anonymous or in the WHERE of your clarified version, is a function that only appears in the definition of a particular verifier, so what difference can it possibly make whether or not it can be partially applied? That's what I can't get my head around.
In your case, none, except wasted keystrokes... after all, if you want to, you can also partially apply tuples. Semantically speaking, adding a tuple introduces another _|_... you can pass both undefined as well as (undefined,undefined) to an uncurried function, but I doubt that's going to bother you. Tuples are commonly used as return type though. The usual style is to write functions curried and use uncurry if you need to pass it tuples for some reason, like in zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith f = (map (uncurry f) .) . zip -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.
participants (2)
-
Achim Schneider
-
michael rice