سؤال

recently, start to learn Scheme (has been a week with my attention), so everything is really confusing and mixed up in my head. now realized that the expression "evaluation" is the main focus in the scheme.. compare with other "procedural language" languages, Scheme is a different world(yes, "functional language").

start to know new language, "back to the basics". I have read some other questions and answers but it is not exactly what I want to know or what I am confusing (for now... maybe later, can be better..)

Question 1. What exactly "evaluation" means in Scheme ? and Scheme also using "evaluation of environment" = putting in stack?? assign memory???. how differ from execution?

Question 2. Is environment similar definition to "agreement , parameters" in a function?

Question 3. "initial value" in Scheme, and "initial value" other procedural programming (c/c++) are same meaning? usually initial value in procedural language is the value you have to assign before the program run... usually 0, null,... the starting address of memory... etc... but my impression in Scheme's "initial value" is a bit different. Scheme "initial value" = the function itself (at least in recursion)... Can anyone explain this clear with very plain expression (no "buzz Scheme" words) ???

Question 4. then it seems you can explain the difference between let, letrec, let* with evaluation. (not sure..yet). something like this (could be completely wrong...),

4-1) ["When a normal "let" is evaluated, the initial value expressions are evaluated before binding is done."] means? = (with my words) when let is executed?? and I don't understand.. this "the initial value expressions are evaluated before binding is done." How it can evaluate before binding is done...?? what exactly mean "binding is done"???

4-2) ["let*": when an order is important, evaluates the environment (parameter?) step by step along with the order program lines(???) ] by this explanation, to me, evaluates = executes.... => (my interpretation, "let*" assign memory and executes for each parameters step by step along with the order of program line???

4-3) "letrec" : ["letrec lets us create an environment before evaluating the initial value(??) expressions, so that the initial value computations execute inside the new environment(my interpretation this sounds for tail recursion)]." => creates an environment before evaluating the initial value, how?

Question 5. There is no initialization, declaration... in Scheme?

Sorry for a long question and it maybe too basic to others. but it will help me clear things out in Scheme.

هل كانت مفيدة؟

المحلول

1 Evaluation

Evaluation is the process of running a bit of code to it's result. eg. (+ 1 2) ;==> 3. In this particular example + is free variable that needs to be in the environment. It might evaluate to the global prcedure + or it might be that it's a lexical variable from code run before this.

2 Environment

Environment is the variables accessable at a certain point. eg.

(define x 10)
(define f1 (lambda (y) (+ x y))
(define f2 (let ((x 5))
              (lamdba (y) (+ x y))))

The lambda forms that cerate f1 and f2 are equal in the sense they do the exact same thing. The difference in the environment.

(f1 3) ; ==> 13
(f2 3) ; ==> 8

The scope of f2's x is no more but it's referenced in the procedure and accessible when running it but not from any other place. An evaluated lambda form (ie. a procedure) is also known as a closure since the variables accessable at the point of evaluation is accessible when applying the result of that lambda form (the procedure).

3 Initial value

When starting up a Scheme program you have an initial global environment. variables like + and cons are defined. I'm not sure this is what you were after. Perhaps you should give an example of what you were thinking?

4 let, let*, letrec, and lambda

You have these in all examples:

(define x 10)
(define f (lambda (x) x)

Everyone of them turns into anonymous lambda function calls. I'll just do one level, but since you can transform let* into let and let into lambda you can transform everyone of these to lots of lambda forms. Here's how:

(let ((a 1) (b 2) ...) body ...) is the same as ((lambda (a b ..) body ...) 1 2 ...) thus this is true:

(let ((x 5) (y (+ x 1)) (f (lambda (x) (if (< 5 x) (f (- x 1)) x))))
  (list x y (f 10))) ; ==> (5 11 9)

(let ((a 1) (b 2)) body ...) is the same as (let ((a 1)) (let ((b 2)) body ...) thus this is true:

(let* ((x 5) (y (+ x 1)) (f (lambda (x) (if (< 5 x) (f (- x 1)) x))))
  (list x y (f 10))) ; ==> (5 6 9)

(letrec ((a 1) (b 2)) body ...) is the same as (let ((a 'undef) (b 'undef)) (let ((tmpa 1) (tmpb 2)) (set! a tmpa) (set! b tmpb)) body ...) thus this is true:

(letrec ((x 5) (y (list x 1)) (f (lambda (x) (if (< 5 x) (f (- x 1)) x))))
  (list x y (f 10))) ; ==> (5 (undef 1) 5)

Note i changed + to list since (+ 'undef 1) won't work. Your Scheme implementation might use any value in place of undef. When to use what? Use ``letrecand namedletfor procedures that you need to recurse, uselet*for variables where you rely on a previous calculation (it keeps you from writing nestedletforms and uselet` as much as you can.

So imagine you have a procedure that is not recursive, a variable and a variable you derive you can use let*:

(let* ((sq (lambda (x) (* x x))) ; given this is a top level it will have global environment
       (x (sq 5))
       (y (sq 10))
       (res (+ x y)) ; here I'm using both x and y
  (do-something res))

5 declarations

There is. If I want to make global variables I do it like this:

(define test 55)
(define fun (lambda (x) (+ test x))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top