Scheme let statement
Solution 1
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 (withset!
) can be used on all variables, and specialized primitives are provided for destructive update inside lists, vectors, strings, etc.
Solution 2
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.
omega
Updated on June 14, 2022Comments
-
omega almost 2 years
In scheme which is a functional programming language, there is no assignment statement. But in a
let
statement(let ((x 2)) (+ x 3))
You are assigning
2
tox
, so why doesn't this violate the principle that there is no assignment statements in functional programming? -
ThePiercingPrince about 10 yearsA functional programming language encourages and offers features for but does not necessarily enforce functional programming.
-
NeoH4x0r about 4 yearsJust because scheme does not enforce a functional-programming styles does not mean that it is incorrect to call it such. It does offer the ability to use imperative operations like set (change the environment to affect state), but it does not offer a complete feature-set like loops, etc therefore Scheme is a functional-programming language, that offers some imperative features.
-
Óscar López about 4 years@NeoH4x0r actually Scheme does offer traditional loops (see the
do
loop) and depending on the dialect, quite a wide variety offor
looping constructs. I'd say it's a quite complete feature set of imperative features :) . Tell me one feature you think it's missing, and I bet I can find it for you! -
NeoH4x0r about 4 years@Óscar López It still does not make it incorrect to call if a function-language (even if it offers some imperative features).