[Haskell-cafe] Newbie question

Bernie Pope bjpop at csse.unimelb.edu.au
Fri Dec 29 21:51:09 EST 2006

On 30/12/2006, at 1:33 PM, Pieter Laeremans wrote:

> Hi,
> I'm reading the Haskell school of expression by Paul Hudok. Great  
> book.

Hudak. And I  concur, a great book.

> However I would like some feedback about a solution to an exercise
> The problem is quite simple :
> define f1 and f2 (using higher order functions ) such that
> f1 (f2 (*) [1..4]) 5 => [5,10,15,20]
> I have come up with the following solution :
> f2 :: (a->b)->[a] -> [b]
> f2 f xs = map f xs

That's fine, but what you are really saying is that f2 is the same as  
map, so you can make
that connection more obvious like this:

    f2 = map

> f1 fs a = map (applyOp a) fs
> applyOp  b f = f b

That's also fine. You can avoid applyOp in numerous ways. One way is  
to use the
function called $ from the Prelude; it implements function application.

f $ x = f x

So you could write:

    f1 fs a = map ($ a) fs

Of course, there are also other ways of implementing f1 and f2 such  
that you get the
desired result, but the map approach seems to be what the question  
was angling for.


More information about the Haskell-Cafe mailing list