From: pjb@... (Pascal J. Bourguignon) Date: 2009-01-11T04:59:50+09:00 Subject: Re: functional programming Brian Candler writes: > Pascal J. Bourguignon wrote: >> Are you saying that you must use lambda everywhere, even when you have a >> named function? That named functions are less than anonymous functions? > > No. I was trying: (1) to understand what the underlying problem was that > you were trying to solve in jumping through all these hoops, rather than > using lambda { ... } directly, lambda makes an anonymous function. I didn't want to use an anonymous function, I wanted to use an existing function. There's a secondary problem I tried to solve: Ruby has no named function, it only has named methods. When you call a method, you have to pass a recipient object as first parameter. > and (2) to understand Mike's assertion > that Ruby lambdas are not first-class functions (i.e. "The difference > between lambda { } and a real first-class function is quite profound"). > Sure, in the latter you'd write smallest[[1,2,3]]. The outer brackets > delimit the call of the lambda, and the inner ones mark the array. Yet another ad-hoc syntax. Where does it come from? How many other such syntaxes are hidden? Can't there be a simple language you can learn in half a day? Yes: Lisp. > I guess you rob Peter to pay Paul. In Ruby, a bareword like "biggest" > can be a method name (in which case it invokes the method, and evaluates > to its return value), or a variable name (in which case it evaluates to > the content of that variable). In Lisp, it can be both, and you always know which you refer. (defun foo (x) (1+ x)) (let ((foo 41)) (foo foo)) --> 42 And if you happen to store functions in a variable, you have the operators FUNCTION and FUNCALL to switch from one namespace to the other: (let ((foo (lambda (x) (1- x)))) (list (funcall (function foo) 0) ; calls the function foo (foo 0) ; idem (funcall foo 0))) ; calls the function bound to the variable foo. --> (1 1 -1) > This means that common cases in Ruby don't need any syntactic markers > like () to say "invoke this method". But parentheses are necessary anyways (see for example the thread about s = "thingy" if ((1 + 1) != 2) ). > The code just looks cleaner. But in > that case, if you want to refer to the *name* of the method or the > *method/function itself* then you need to mark in that case instead. > i.e. :foo or method(:foo) -- __Pascal Bourguignon__