Scheme let statement

六眼飞鱼酱① 提交于 2019-12-04 05:16:48

The statement "Scheme which is a functional programming language" is incorrect. In Scheme, a functional-programming style is encouraged, but not forced. In fact, you can use set! (an assignment statement!) for modifying the value of any variable:

(define x 10)
(set! x (+ x 3))
x
=> 13

Regarding the let statement of the question, remember that an expression such as this one:

(let ((x 10))
  (+ x 3))
=> 13

... it's just syntactic sugar, and under the hood it's implemented like this:

((lambda (x)
   (+ x 3))
 10)
=> 13

Notice that a let performs one-time single assignments on its variables, so it doesn't violate any purely functional programming principle per se, the following can be affirmed of a let expression:

An evaluation of an expression does not have a side effect if it does not change an observable state of the machine, and produces same values for same input

Also, quoting from Wikipedia:

Impure functional languages provide both single assignment as well as true assignment (though true assignment is typically used with less frequency than in imperative programming languages). For example, in Scheme, both single assignment (with let) and true assignment (with set!) can be used on all variables, and specialized primitives are provided for destructive update inside lists, vectors, strings, etc.

http://en.wikipedia.org/wiki/Assignment_(computer_science)#Single_assignment

Basically, it's a single assignment that's allowable. Other assignment is not "allowed" because of side effects.

Edit: allowed in quotations because, as Oscar stated, it is not mandatory but suggested.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!