I am needed to use map
on a list of lists in Scheme (Racket).
What I want specifically is something like (map-lists add1 '((3 1) (2 8))) => '((4 2) (3 9))
.
I know how to solve this recursively, like so:
(define map-lists
(lambda (f A)
(if (null? A)
'()
(if (pair? (car A))
(cons (map f (car A)) (map-lists f (cdr A)))
(if (null? (car A))
(cons (car A) (map-lists f (cdr A)))
(cons (f (car A)) (map-lists f (cdr A))))))))
But am needing to solve this without recursion! I know (map add1 '(3 1))
will give '(4 2)
which is part of the solution, but this doesn't work for a list of lists.
Please help.
You are reimplementing one map
and use another inside it.
You can just use two map
s for the two levels of the nested lists. Consider this:
(map 1+ (list 2 3)) ==> (3 4)
(lambda (x) (map 1+ (list 2 3))) #f ==> (3 4)
(lambda (x) (map 1+ x)) (list 2 3) ==> (3 4)
(lambda (x) (map 1+ x)) (list 7 8) ==> (8 9)
(map (lambda (x) (1+ x)) (list 2 3)) ==> (3 4)
(map (lambda (x) (map 1+ x)) (list (list 2 3) (list 7 8))) ==> ....
Hopefully you can complete the rest.