Reputation: 3
I am very new to Scheme and I am slowly finding my way around it.
I have some doubts on Procedures and Map which I hope could be answered.
(map plus1 (list 1 2 3 4))
will basically return me the result:
(2 3 4 5)
It is fine if the procedure takes in the list as its only parameter. My question is how am I able to use a procedure like such which takes in 2 parameters with Map?
(define plus(m list)
(+ m list))
Thanks in advance for help and advice.
Upvotes: 0
Views: 5061
Reputation: 723
Use two lists. See what happens:
guile> (map (lambda (x y) (+ x y)) '(1 2 3) '(4 5 6))
(5 7 9)
Upvotes: 0
Reputation: 370435
(define (plus m xs)
(map (lambda (x) (+ m x)) xs))
or
(define (adder m)
(lambda (x) (+ m x)))
(define plus (m xs)
(map (adder m) xs))
Which allows you to reuse the adder
function for other things as well.
Upvotes: 1
Reputation: 191
Maybe like this?
(define (plus m n) (+ m n))
(map plus (list 1 2 3) (list 4 5 6))
; => (list 5 7 9)
Upvotes: 3