Map in terms of fold

Benutzeravatar
sewe
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 295
Registriert: 16. Jan 2009 14:53
Kontaktdaten:

Map in terms of fold

Beitrag von sewe »

In todays exercise class the question arose how to implement map in terms of fold and - if I understood the question correctly - to not use explicit function arguments (as in the following):

Code: Alles auswählen

map f l = foldr ((:) . f) [] l
This obviously simplifies to the definition below:

Code: Alles auswählen

map f = foldr ((:) . f) []
Eliminating the very last argument to map, f, is tricky, however:

Code: Alles auswählen

map = flip foldr [] . ((:) .)
So, look up flip and (.) in the Haskell Prelude and try to decipher it. :wink:

Zurück zu „Archiv“