Skip main navigation

Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. T&Cs apply

Functional Maps and Folds versus Imperative Loops

Haskell provides powerful higher-order functions such as map and fold. Wim Vanderbauwhede explains how they relate to loops in imperative languages.

This tutorial explains the relationship between the higher-order list operations map, foldl and foldr and loops in an imperative language.

The source code can be found on GitHub

Summary

map : loop over list element-by-element, append new element to new list

foldl : loop over list element-by-element, update accumulator using current accumulator and element

foldr : loop over reverse list element-by-element, update accumulator using current accumulator and element

Note:

 map :: (a -> b) -> [a] -> [b]
foldl :: (b -> a -> b) -> b -> [a] -> b
foldr :: (a -> b -> b) -> b -> [a] -> b
This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life