0

I am now writing a scheme's interpreter by using c++. I got a question about define and lambda.

(define (add x y) (+ x y))

is expanded as

(define add (lambda (x y) (+ x y)))

by lispy.py

what's the difference between this two expression?

why need expand the expression? Just because it is easy to evaluate the expression?

harborn
  • 15
  • 2

2 Answers2

4

They are the same, since one gets expanded into the other. The first expression is easier to both write & read; having it expand into the second simplifies the interpreter (something you should appreciate).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • It's *likely* that "one gets expanded into the other", but that's not a requirement. The standard (R5RS, at least) just says that they're equivalent. An implementation could choose to recognize both without doing any syntactic expansion. – Joshua Taylor Jan 05 '15 at 22:26
  • 1
    @JoshuaTaylor: Was going by what was stated in OP. – Scott Hunter Jan 06 '15 at 00:56
  • Yes, I notice now that OP is citing a specific interpreter that's a sort of Lisp-language, and not necessarily exactly Scheme. The question is tagged with [tag:sicp], which suggests a proper Scheme (hence why [I appealed](http://stackoverflow.com/a/27788961/1281433) to the R5RS standard), but it looks like [tag:sicp] may well be a mis-tag. – Joshua Taylor Jan 06 '15 at 02:08
1

They are equivalent in R5RS:

5.2 Definitions

(define (<variable> <formals>) <body>)

<Formals> should be either a sequence of zero or more variables, or a sequence of one or more variables followed by a space-delimited period and another variable (as in a lambda expression). This form is equivalent to

(define <variable>
  (lambda (<formals>) <body>)).
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353