Lambda evaluation - postfix or prefix?

Alex Blewitt alex.blewitt at gmail.com
Fri Jan 22 18:15:48 PST 2010


The 0.1 spec that Alex Buckley sent out proposed a separate syntax for  
invocation of Lambda expressions:

LambdaInvocationExpression
  LambdaExpression LambdaInvocation

LambdaInvocation:
  '!' '(' ArgumentList_opt ')'
  '.' '(' ArgumentList_opt ')'

In other words,

  #()(42)!()  // Evaluates to the value 42.

  #int() fortyTwo = #()(42);
  assert fortyTwo!() == 42;

I don't know of a language which uses postfix symbols on a function as  
a determination of whether it's a first-order-function or not.  
However, I'd like to propose that we borrow 'apply' from other  
functional languages and use this as a prefix instead:

LambdaInvocationExpression
  'apply' LambdaExpression '(' ArgumentList_opt ')'

This would allow the above examples to be written as

apply #()(42) ()
apply fortyTywo ()

These could be nested, such that the result of one function was the  
result of another, e.g.

##int(int)(int) plus = #int(int x) #(int y) x + y

apply (apply plus (1)) (1)

To reduce verbosity, one could use a symbol instead of 'apply'. For  
example, we could re-use the # symbol:

##plus(1)(1)

Here, the distinguishing feature is that 'plus' is not a valid type,  
and that instead it refers to a lambda expression name in the scope.  
However, it may be desirable to use a different symbol for this, so  
taking it as @pply, we could have:

@@plus(1)(1)

So one could think of '@' as the 'apply' or 'invocation' operator.  
Reading from left to right gives 'apply "the result of apply plus to  
one", to one'

#(String s) alblue = #(String s) new Twitter("alblue").tweet(s);

if (youLikeThis) {
   @alblue("Great idea!")
} else {
   @alblue("That sucks!")
}


More information about the lambda-dev mailing list