how to expand list-map

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

how to expand list-map

Post 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

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: how to expand list-map

Post 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.
(λx. x x) (λx. x x)

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: how to expand list-map

Post by jopython »

Ricky,

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

Locked