You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Map, filter, and reduce (called fold in Haskell) are the three most important higher-order functions in functional programming. They provide powerful, concise ways to transform and process lists without writing explicit loops.
Map applies a function to every element of a list, producing a new list of the same length with the transformed values.
map f [a, b, c, d] = [f(a), f(b), f(c), f(d)]
# Double every number
numbers = [1, 2, 3, 4, 5]
doubled = list(map(lambda x: x * 2, numbers))
# doubled = [2, 4, 6, 8, 10]
# Convert temperatures from Celsius to Fahrenheit
celsius = [0, 20, 37, 100]
fahrenheit = list(map(lambda c: c * 9/5 + 32, celsius))
# fahrenheit = [32.0, 68.0, 98.6, 212.0]
# Get lengths of strings
words = ["hello", "world", "hi"]
lengths = list(map(len, words))
# lengths = [5, 5, 2]
-- Double every number
doubled = map (*2) [1, 2, 3, 4, 5]
-- doubled = [2, 4, 6, 8, 10]
-- Convert to uppercase
import Data.Char (toUpper)
upper = map toUpper "hello"
-- upper = "HELLO"
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.