; map and fold "higher-order" functions ; The function 'map' is a function that ; takes a function and a list as arguments, ; and applies (maps) the function over the ; elements of the list, returning a list. ; It is written recursively as: (define map (lambda (f ls) (if (null? ls) '() (cons (f (car ls)) (map f (cdr ls)))))) ; Examples (map car '((1 2 3) (1) (4 5))) (map cdr '((1 2 3) (1) (4 5))) (map length '((1 2 3) (1) (4 5))) (define (square x) (* x x)) (map square '(1 2 3 4 5)) ;; or we can just write an inline lambda expression (map (lambda (x) (* x x)) '(1 2 3 4 5))