newLISP Fan Club

Forum => newLISP in the real world => Topic started by: jopython on August 17, 2014, 11:40:00 AM

Title: how to expand list-map
Post by: jopython on August 17, 2014, 11:40:00 AM
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?



(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
Title: Re: how to expand list-map
Post by: rickyboy on August 17, 2014, 03:09:25 PM
Just use (args).  (Other lisps call them rest args.)


> (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:


> (map + '(1 2 3) '(4 5 6) '(7 8 9))
(12 15 18)

Hence, map is sufficient for the job.
Title: Re: how to expand list-map
Post by: jopython on August 17, 2014, 05:51:38 PM
Ricky,



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

Thanks