Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Let/lambda equivalence

Name: Anonymous 2012-08-13 20:54

Most call-by-value languages with anonymous functions have let/lambda equivalence: declarations are exactly the same as function parameters/arguments.
((lambda (i j) (* i j)) 3 4)
(let ((i 3) (j 4)) (* i j))


((INT i, j)INT: i * j)(3, 4)
(INT i = 3, j = 4; i * j)


(fn (i, j) => i * j)(3, 4)
let val (i, j) = (3, 4) in i * j end


A related rule is that any expression can be wrapped in a lambda without changing its meaning (besides grouping the expression). Notice how it is the expressions within the lambda bodies that create the scope in which the variables are bound and not the lambdas themselves.
((lambda () (let ((i 3) (j 4)) (* i j))))
INT: (INT i = 3, j = 4; i * j)
(fn () => let val (i, j) = (3, 4) in i * j end)()


One popular language does not follow these rules. Can you guess what it is? It's a language where certain constructs need to be wrapped in functions which are called immediately. It's supposedly ``Scheme-inspired'' but it's statement-based and uses a return keyword. The upcoming version will add a new type of function and variable that does follow these rules, at the expense of having two sets of rules and bloating the language beyond C++.
Give up? It's JavaScript.

Name: Anonymous 2012-09-19 1:14

>>30

Dynamic scoping can be useful if it is manageable. If you could turn it on for only a few variables, and treat them with the same discretion used with global variables, I could see it working. The trouble is that it requires coordination between the function that references the variable and some parent function that happened to initialize the variable. This becomes very difficult to keep track of when it is used more and more. It's even worse if you are allowed to modify the dynamically scoped variables. You might depend on the global variable being changed, but instead some local variable in the middle of the call stack got it, and then was destroyed when it went of scope. Trying to do anything in a language where things like that can happen unpredictably is just terrible. It could be ok if you have to explicitly declare variables to have dynamic scope.

You can always get the same power as dynamic scope by explicitly passing the variable down to the child functions through the parameters. It is verbose, but it accomplishes getting the values to where they are needed and you have the flexibility of renaming parameters or changing them as they are passed down. If you have a lot of values to pass, you can wrap them in an object and pass around with a single reference.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List