9. my-map Write a function named my-map that works like the map function provide
ID: 3625242 • Letter: 9
Question
9. my-map
Write a function named my-map that works like the map function provided by the Scheme library and
demonstrated below. For example,
> (my-map (lambda (x) (+ x 1)) ’(1 2 3 4 5))
(list 2 3 4 5 6)
Here’s a definition of map
(define (map proc items)
(if (null? items)
nil
(cons (proc (car items))
(map proc (cdr items)))))
Using above definition:
> (map abs (list -10 2.5 -11.6 17))
(10 2.5 11.6 17)
> (map (lambda (x) (* x x)) (list 1 2 3 4))
(1 4 9 16)
12.
Define a procedure named zip that takes two lists and returns a list of pairs of the elements in the argument
> (zip (
((a 1) (b 2) (c 3))
3
lists. If the argument lists are of unequal lengths, the returned list should be truncated to the length of the
shorter list.
Explanation / Answer
#9 This question was a little strange since it seems like they basically give you the answer, but this does do what it's supposed to.
(define (my-map proc items)
(if (null? items)
'()
(cons (proc (car items))
(my-map proc (cdr items)))))
#12
(define (zip l1 l2)
(if (null? l1) '()
(if (null? l2) '()
(cons (list (car l1) (car l2)) (zip (cdr l1) (cdr l2))))))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.