s o
s o

Reputation: 83

Who can explain this Haskell function?

The Haskell function

foo = zipWith ($) . repeat

does exactly the same as

map

but I cannot see why :-( Who can give an explanation? Thx a lot!

Upvotes: 8

Views: 128

Answers (1)

MathematicalOrchid
MathematicalOrchid

Reputation: 62808

OK, so we have

foo = zipWith ($) . repeat

which is the same as

foo f = zipWith ($) (repeat f)

The repeat f generates an infinite list of copies of f. Then zipWith uses the ($) operator to apply each element of the [infinite copies of f] list to each element of the incoming list. Which is what map does.

Yes?

Upvotes: 14

Related Questions