Are there any programming languages ​​where variables are really functions?

For example, I would write:

x = 2
y = x + 4
print(y)
x = 5
print(y)

And he will output:

6 (=2+4)
9 (=5+4)

In addition, are there cases where this can actually be useful?

Specification: Yes, lambda, etc. solve this problem (they were, as I came to this idea); I was wondering if there are certain languages ​​in which it was by default: no functions or lambda keys are required.

+3
source share
10 answers

Haskell , , ( , x ).

y = x + 4 , y = map (+4) [1..] ( 4 1 ), y? , , ? (: ). , , .

, Haskell y - , x+4, x , , 2.


, .

Lua:

x = function() return 2 end
y = function() return x() + 4 end
print(y())
x = function() return 5 end
print(y())
$ lua x.lua
6
9

Python ( , x):

x = lambda: 2

y = lambda: x() + 4

print(y())

x = lambda: 5

print(y())
$ python x.py
6
9
+7

func #

Func<int, int> y = (x) => x + 5;
Console.WriteLine(y(5)); // 10
Console.WriteLine(y(3)); // 8

... ...

int x = 0;
Func<int> y = () => x + 5;
x = 5;
Console.WriteLine(y()); // 10
x = 3;
Console.WriteLine(y()); // 8

... , , , .

  • , .
  • .
+4

, F #, Haskell Scala. Scala , apply(), , . , Scala .

: , , , , Lisps (, Scheme).

+2

.

. 60 , , .

+1

Lucid , x y "", . :

y
  where
    y = x + 4
  end

:

 x(0): 2
   y = 6
 x(1): 5
   y = 7

, Lucid ( ) , , . ( )

+1

F # Wikipedia .

, , , F # .

, !

0

, , . (Tradestation, metastock ..), (, /), , , , .

My 2c: , , , , . , r. (javascript) , r- (, , ) n-fold:

var x = 2;
var y = function() { return x+2; }
alert(y());
x= 5;
alert(y());
0

Self , . ( ), .

0

Scala .

def foo(x : => Int) {
    println(x)
    println(x) // x is evaluated again!
}

, .

0

, , Octave, R Maxima. , , , .

0

Source: https://habr.com/ru/post/1719474/


All Articles