6

I am learning Scheme. What is wrong with the code below?I want to write a program that takes the first function from the list and then applies that to a number?

    (define num  3)

    ;;I want to do something like this which returns 3
    ((λ (x) x)num)

    ;;but my functions are in a list so this should return3
    ((first '((λ (x) x) (λ (x) (* x x)))) num)

Im getting this error for the above code:
procedure application: expected procedure, given: (λ (x) x); arguments were: 3

What does it mean when I get these kinds of output?

When I dont apply anything, I get a nice output.

(first '((λ(x) x)(λ(x) (*x x))))

returns (λ (x) x)

3 Answers 3

11

You're quoting, with ' the lambda, so it isn't being evaluated.

If you just feed in (λ (x) x) at the prompt, DrScheme shows you #<procedure>, which means it has actually evaluated the lambda, and given you back a closure. By quoting it, you're giving Scheme just a list of symbols.

If you want to put your functions in a list, you can do:

((first (list (lambda (x) x) (lambda (x) (* x x)))) num)

The quote allows you to produce a list, yes, but one whose contents aren't evaluated. The list function produces a list from all of its arguments, after they've been evaluated.

You could also quasiquote the list, if you like:

((first `(,(lambda (x) x) ,(lambda (x) (* x x)))) num)
0
2

What is the difference between these expressions?

> (procedure? (lambda (n) n))
#t
> (procedure? (quote (lambda (n) n)))
#f
> (procedure? '(lambda (n) n))
#f

Jay answered it for you but I can't upvote him yet.

2

(lambda (x) x) isn't a procedure. It's a form that evaluates to a procedure. People are a bit loose with terminology and often call the lambda form a procedure as a kind of shorthand. “Ceci n'est pas une pipe.”

5
  • True, but ultimately useless as an explanation for newbies that are confused by such issues. Note BTW that since then Racket changed the way it prints values, which results in a clearer error: "... expected procedure, given: '(λ (x) x) ..." -- the quote is new. Commented Jul 7, 2011 at 15:53
  • 1
    When I was a newbie this was exactly the sentence I needed to clear up the confusion, so I wrote it down to help others. Perhaps it's just "ultimately useless" for everyone but me.
    – rptb1
    Commented Jul 13, 2011 at 20:02
  • 1
    The thing is that Scheme has some (very intentional!) confusion between code and data, x and 'x, your explanation is correct (as I said) but it is a similar kind of confusion at the meta level, where x is data and "the value bound to x" is the "code". For many students, this kind of duality is very confusing, and mostly does not get completely clear until they go through something like the meta-circular evaluator. Commented Jul 14, 2011 at 2:20
  • It gets worse, since it's more like “the variable cell bound to x containing the value of x” :P
    – rptb1
    Commented Jul 14, 2011 at 17:32
  • Yes, I intentionally avoided the extra indirection that you get when you introduce mutations and locations. Commented Jul 15, 2011 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.