views:

329

answers:

10

For example, I would write:

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

And it would output:

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

Also, are there any cases where this could actually be useful?

Clarification: Yes, lambdas etc. solve this problem (they were how I arrived at this idea); I was wondering if there were specific languages where this was the default: no function or lambda keywords required or needed.

+2  A: 

Check out various functional languages like F#, Haskell, and Scala. Scala treats functions as objects that have an apply() method, and you can store them in variables and pass them around like you can any other kind of object. I don't know that you can print out the definition of a Scala function as code though.

Update: I seem to recall that at least some Lisps allow you to pretty-print a function as code (eg, Scheme's pretty-print function).

Jim Ferrans
A: 

Try checking out F# here and on Wikipedia about Functional programming languages.

I myself have not yet worked on these types of languages since I've been concentrated on OOP, but will be delving soon once F# is out.

Hope this helps!

mallows98
F# has been released as far as I understand it, it's just not an official part of Visual Studio till 2010
Matthew Scharley
It hasn't been released - the versions for 2008 are still "CTP".
Pavel Minaev
A: 

The closest I've seen of these have been part of Technical Analysis systems in charting components. (Tradestation, metastock, etc), but mainly they focus on returning multiple sets of metadata (eg buy/sell signals) which can be then fed into other functions that accept either meta data, or financial data, or plotted directly.

My 2c: I'd say a language as you suggest would be highly confusing to say the least. Functions are generally r-values for good reason. This code (javascript) shows how enforcing functions as r-values increases readability (and therefore maintenance) n-fold:

var x = 2;
var y = function() { return x+2; }
alert(y());
x= 5;
alert(y());
cmroanirgo
+3  A: 

you can use func expressions in C#

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

... or ...

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

... if you are really wanting to program in a functional style the first option would probably be best.

  1. it looks more like the stuff you saw in math class.
  2. you don't have to worry about external state.
Matthew Whited
+7  A: 

Haskell will meet you halfway, because essentially everything is a function, but variables are only bound once (meaning you cannot reassign x in the same scope).

It's easy to consider y = x + 4 a variable assignment, but when you look at y = map (+4) [1..] (which means add 4 to every number in the infinite list from 1 upwards), what is y now? Is it an infinite list, or is it a function that returns an infinite list? (Hint: it's the second one.) In this case, treating variables as functions can be extremely beneficial, if not an absolute necessity, when taking advantage of laziness.

Really, in Haskell, your definition of y is a function accepting no arguments and returning x+4, where x is also a function that takes no arguments, but returns the value 2.


In any language with first order functions, it's trivial to assign anonymous functions to variables, but for most languages you'll have to add the parentheses to indicate a function call.

Example Lua code:

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

Or the same thing in Python (sticking with first-order functions, but we could have just used plain integers for x):

x = lambda: 2

y = lambda: x() + 4

print(y())

x = lambda: 5

print(y())
$ python x.py
6
9
Mark Rushakoff
Algol68 (1968) is strong typed, and allows the declarations of function variables, eg: MODE FUN = PROC INT; FUN x := INT:2; FUN y := INT:x + 4; print(y); x := INT:5; print(y) ... Produces: +6 +9 ... c.f. Sourceforge: [https://sourceforge.net/projects/algol68 ]
NevilleDNZ
+1  A: 

This is the way spreadsheets work.

It is also related to call by name semantics for evaluating function arguments. Algol 60 had that, but it didn't catch on, too complicated to implement.

starblue
A: 

Self makes no distinction between fields and methods, both are slots and can be accessed in exactly the same way. A slot can contain a value or a function (so those two are still separate entities), but the distinction doesn't matter to the user of the slot.

Joachim Sauer
A: 

In Scala, you have lazy values and call-by-name arguments in functions.

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

In some way, this can have the effect you looked for.

Dario
A: 

I believe the mathematically oriented languages like Octave, R and Maxima do that. I could be wrong, but no one else has mentioned them, so I thought I would.

Teddy
+1  A: 

The programming language Lucid does this, although it calls x and y "streams" rather than functions. The program would be written:

y
  where
    y = x + 4
  end

And then you'd input:

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

Of course, Lucid (like most interesting programming languages) is fairly obscure, so I'm not surprised that nobody else found it. (or looked for it)

Mathnerd314