Page 1 of 1

how to expand list-map

Posted: Sun Aug 17, 2014 6:40 pm
by jopython
The following function performs a map over two lists.
How to extend this abstraction where the numbers of lists are unknown instead of only two?

Code: Select all

(define (list-map op lst1 lst2) 
      (map (fn (x y) (op x y)) lst1 lst2))
For. eg
I should be able to call
(list-map + '(1 2 3) '(3 4 4))
or

(list-map + '(1 2 3) '(5 6 7) '(8 9 10))

or even more number of lists as arguments

Re: how to expand list-map

Posted: Sun Aug 17, 2014 10:09 pm
by rickyboy
Just use (args). (Other lisps call them rest args.)

Code: Select all

> (define (my-silly-func) (cons 'op (args)))
(lambda () (cons 'op (args)))

> (map my-silly-func '(1 2 3))
((op 1) (op 2) (op 3))

> (map my-silly-func '(1 2 3) '(4 5 6))
((op 1 4) (op 2 5) (op 3 6))

> (map my-silly-func '(1 2 3) '(4 5 6) '(7 8 9))
((op 1 4 7) (op 2 5 8) (op 3 6 9))
Your example, with + is just this:

Code: Select all

> (map + '(1 2 3) '(4 5 6) '(7 8 9))
(12 15 18)
Hence, map is sufficient for the job.

Re: how to expand list-map

Posted: Mon Aug 18, 2014 12:51 am
by jopython
Ricky,

My ultimate goal was to abstract "map +" into a single function
Thanks