;===== ; ASIDE: during CS 111 - Week 6 Lecture 2 - 2026-10-01 ;===== ; The map function ;===== ; NOTE: Change DrRacket's Language to "Intermediate Student" ; to RUN this example! ;===== ; Remember Week 6 Lecture 1's function add1-list? ; that adds one to each element of a list of numbers? ; ; It turns out that the idea of APPLYING some function to EACH element in a ; list to make a NEW list is useful enough that one might like to be able ; to write an expression "apply function so-and-so to each element of ; list so-and-so and give me a list of the results" ; ; Such a capability is indeed sometimes provided by languages that allow you ; to pass a function as an argument, such as Racket, Python, JavaScript, ; and more; ; ; It is often a function named map. ; (you are MAPPING the given function to EACH element in the given list) (map add1 (list 1 3 5 7 9 11 13 15 17 19)) (equal? (map add1 (list 1 3 5 7 9 11 13 15 17 19)) (list 2 4 6 8 10 12 14 16 18 20)) (map sub1 (cons 3 (cons 6 (cons 9 empty)))) (equal? (map sub1 (cons 3 (cons 6 (cons 9 empty)))) (cons 2 (cons 5 (cons 8 empty))))