Darran
Darran

Reputation: 3

Usage of Procedure and Map in Scheme

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

Answers (3)

Joel J. Adamson
Joel J. Adamson

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

sepp2k
sepp2k

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

Robby
Robby

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

Related Questions