views:

65

answers:

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.

+1  A: 
(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.

sepp2k
You should not clobber `list`. Scheme has only a single namespace for functions and other values.
Svante
@Svante: a) It's a local variable, so all I clobber is the local scope of the plus method. b) I just used the names that Darran used. But fine, I changed it.
sepp2k
Okay, I'll shut up about my pet peeve: a single lower-case l as a parameter looks like a 1. It's fine, leave it.
JasonFruit
@Jason: Not it's fine, I'm happy to change my variable names in response to comments. I'm nothing if not accommodating.
sepp2k
You have my gratitude.
JasonFruit
This is exactly what i was looking for. Thanks! Just so I understand, it is just creating a function to call the Map method and then defining a anonymous function to do the addition?
Darran
@Daran: Yes, exactly.
sepp2k
+1  A: 

Maybe like this?

(define (plus m n) (+ m n))

(map plus (list 1 2 3) (list 4 5 6))

; => (list 5 7 9)

Robby
A: 

Use two lists. See what happens:

guile> (map (lambda (x y) (+ x y)) '(1 2 3) '(4 5 6))
(5 7 9)
Joel J. Adamson