how to expand list-map

Q&A's, tips, howto's

how to expand list-map

Postby jopython » Sun Aug 17, 2014 6:40 pm

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
jopython
 
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: how to expand list-map

Postby rickyboy » Sun Aug 17, 2014 10:09 pm

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)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: how to expand list-map

Postby jopython » Mon Aug 18, 2014 12:51 am

Ricky,

My ultimate goal was to abstract "map +" into a single function
Thanks
jopython
 
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron