;; built-in map takes N lists, with the function taking N arguments ;; - we usually just see this where N=1, so it's (map f l) Welcome to DrScheme, version 206p1. Language: Standard (R5RS). > map # > (map (lambda (x) (+ x 1)) '(3 5)) (4 6) > (map (lambda (x y) (+ x y)) '(3 5) '(2 10)) (5 15) ;; Other functions, like + > (+ 3 5 19 30) 57 > (+) 0 ;; You can define these yourself by just leaving out the ()s in ;; the lambda: > (lambda l (f l)) # ;; Here's an example (this wasn't done in class...) ;; This function is like "list" but it reverses the result > (define revlist (lambda l (reverse l))) # > (revlist 1 2 3) (3 2 1) ;; Note that the number of lists must agree with the function, and ;; the lists needs to be the same length, and they need to be lists > (map (lambda (x y) (+ x y)) '(3 5)) . map: arity mismatch for procedure 15:7: expects 2 arguments, given 1 > (map (lambda (x y) (+ x y)) '(3 5) '(4)) . map: all lists must have same size; arguments were: # (3 5) (4) > (map (lambda (x) (+ x 1)) (cons 2 3)) . map: expects type as 2nd argument, given: (2 . 3); other arguments were: # > (cons 2 3) (2 . 3) > (map (lambda (x) (+ x 1)) 2) . map: expects type as 2nd argument, given: 2; other arguments were: # ;; You can do something like this last thing using foldr, though: > (define (foldr f l base) (if (null? l) base (f (car l) (foldr f (cdr l) base)))) > (foldr + '(1 3 5) 4) 13 ;; Here is another example of map with multiple arguments.. > (map list '(1 2 3) '(4 5 6) '(7 8 9)) ((1 4 7) (2 5 8) (3 6 9)) ;; --------------------------------------------- (define (addN n l) (map (lambda (x) (+ x n)) l)) Welcome to DrScheme, version 206p1. Language: Standard (R5RS). > (addN 4 '(1 3 5)) (5 7 9) > (lambda (x) (+ x n)) # ;; n is undefined in this example > ((lambda (x) (+ x n)) 3) . reference to undefined identifier: n ;; but it is defined here > (let ((n 2)) ((lambda (x) (+ x n)) 3)) 5 ;; If we define a and b to be these two procedures > (define a (let ((n 2)) (lambda (x) (+ x n)))) > (define b (lambda (x) (+ x n))) > a # > b # ;; We see that a is ok, as it stores x=3 in the closure, but b ;; still is missing a value for n > (a 3) 5 > (b 3) . reference to undefined identifier: n