Jay.F
Jay.F

Reputation: 101

Is this a usable scheme function?

Is this function from my lecture notes written correctly?

(define foo (λ (f) (+ (f 1) (f 2))))

(foo 3) doesnt work and any more arguments says it expects 1 argument. I assume the lecturer meant:

(define foo (λ (f) (+ f f)) )

As far as I know (f 1) isnt a valid way of saying anything in scheme but if the first is actually a correct function can anyone give me an example of a valid input?

Upvotes: 2

Views: 49

Answers (2)

Jay.F
Jay.F

Reputation: 101

Update: I found this example so the function is correct as is. I am actually not sure what is happening here though.

(define foo (λ (f) (+ (f 1) (f 2))))

(foo (λ (x) (* x 2)))

Output: 6

Edit: nevermind as I was typing I eventually understood the syntax. If the parameter f is a function, then (f 1) would mean f with an input of one and then the example makes sense. Should I delete the question probably wont be of use to anyone?

Upvotes: 0

river
river

Reputation: 1028

(define foo (λ (f) (+ (f 1) (f 2))))

is ok. It takes a function f as an input. You can call it like this:

(define g (lambda (x) (* x 10)))
(foo g)

Upvotes: 4

Related Questions