Small SICP/Scheme question (local state) -


I'm actually reading a book for fun but it can be considered homework. In any event, I do not feel comfortable with the local state variables with this language ... For example, use this code:

  (define flip (0 Counting (counting) counting (counting counting number 0)) (Lambda () (lambda) (if (= 0 count) (start (count! Count 1)) (start (count! Count))))))  

Why is code optional between 1 and 0? The value of count 0 is called this function every time! A dragon will be equivalent:

  class flip: def __init __ (self): pass def __call __ (self): count = 0 if count = 0 0: count = 1 return count else: count = 0 Return calculation  

It gives every single thing every time I am confused ...

I have a little experience writing compiler for functional languages, so perhaps a brief description of how memory is represented is in order. Each function can be broadly considered as a pair (E, F) where e is the set of free variables, and the "code" of the F function itself is itself. When you call a function, then it takes value in e and changes those options for variables in F, and then executes the code using those values.

Therefore, where your example is concerned, you have defined "flip" variable to bring the function back to your "expression". This function is the stuff within your lambda. Since "counting" is defined outside the lambda, it is a free variable, so it is stored in the environment of the function. Then, every time you call (flip), the interpreter goes to the code in Lambda, sees that it needs to see the value of "count" in the environment, it does, it changes, and gives returns is. This is the reason that every time you say it, the value stored in "counts" remains.

If you want each time you call on the flip, then you put expression inside Lambda, then it's a bound variable rather than a free variable.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -