[Haskell-cafe] Re: Functions that return functions
Achim Schneider
barsoap at web.de
Sun Apr 12 12:01:30 EDT 2009
michael rice <nowgate at yahoo.com> wrote:
> makeVerifier :: (Int -> Int ->__ Int) -> Int -> (Int -> Bool)
>
> makeVerifier f m = \n -> let d = digits n
>
> ________________________________________________________ i = [1..(length d)]
>
> ________________________________________________ in \n -> divides m (foldl (+) 0 (map2 f i d))
>
makeVerifier :: (Int -> Int ->__ Int) -> Int -> Int -> Bool
makeVerifier f m n = divides m $ foldl (+) 0 $ map2 f i d
where
d = digits n
i = [1..length d]
...looks way more like Haskell[1] to me, and is equivalent (modulo
actually trying it out, and the strange fact that the second n is
unused).
From a scheme perspective, all Haskell functions only take one argument
and are made into multiple-arg functions by stacking lambdas, hidden by
syntactic sugar. The usual (define (foo a b) ...) would be written
"foo (a,b) = ..." in Haskell. In other words, functions are curried by
default.
You might also want to use foldr or foldl' instead of foldl, there's
some differences in behaviour between scheme and Haskell due to
laziness: See http://www.haskell.org/haskellwiki/Fold as well as the
referenced wiki page.
Hope that helps.
[1] There's some decent potential for point-free style there, but I
don't feel like doing that right now
--
(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.
More information about the Haskell-Cafe
mailing list