Skip main navigation

Hurry, only 10 days left to get one year of Unlimited learning for £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now