From: Brian Candler Date: 2009-01-11T05:57:21+09:00 Subject: Re: functional programming Pascal J. Bourguignon wrote: > lambda makes an anonymous function. I didn't want to use an anonymous > function, I wanted to use an existing function. But you can store the lambda under a name. My knowledge of Lisp is very limited, but according to my copy of Abelson and Sussman (2nd ed, pp 62-63), (define (plus4 x) (+ x 4)) "is equivalent to" (define plus4 (lambda (x) (+ x 4))) So I don't see what's wrong with writing plus4 = lambda { |x| x + 4 } in Ruby, when that's all you're doing in Lisp. (Or Plus4, if you consider a constant to be more in the spirit of 'define'). But we could be talking at Scheme/Common Lisp cross-purposes here. > Can't there be a simple language you can > learn in half a day? Yes: Lisp. Well, it's easy for a Lisp programmer to say that "Lisp is simple". It's also easy for me, as a programmer who started in machine code, assembly language and C, to say that attempting to write anything substantial in Lisp makes my head spin. These are arguably statements about ourselves, rather than about the languages. Ruby has syntax to expose various functionality. Lisp has special forms like (quote ...) and (define ...) and (set! ... ) to expose similar functionality. To you, the fact that completely different semantics can be obtained using the same syntax as function application is a benefit. To me, it is a hindrance. It also may boil down to trivia such as which editor we use. One probably can't write substantial Lisp without the help of an editor that understands the syntax (and definitely Lisp's simple syntax is a big benefit there). But to me, emacs is totally impenetrable. I can't even exit the damn thing without going across to another shell and issuing a 'kill'. I use joe and vi, and am very happy with both for what I do. But that in turn probably means my hands are tied against 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 But isn't that the same distinction I was talking about, to refer to a function as a value, versus applying it to some arguments? Lisp: foo versus (foo) C: foo versus foo() Ruby: method(:foo) versus foo (for methods) foo versus foo.call() or foo[] (for lambdas) Most of the time you're invoking methods, not passing them around as first order functions. And then mostly you're invoking a single method with a single set of arguments. Should methods and blocks/lambdas be the same thing in Ruby? Maybe. But actually, the distinction works well in practice. There are few rules you have to learn early on when programming in Ruby, but one of those is how it disambiguates between a local variable and a method call. The rule only works because 'def' starts a new scope. This saves a lot of brackets, and without having to declare local variables. > 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) Unless you are talking about Scheme where they share the same namespace. Anyway, we've strayed way off the track. I will summarise as follows: 1. Ruby is rubbish because it has wrinkly syntax. 2. LISP is rubbish because there are a zillion incompatible versions of it, and is impossible to write without a list-aware editor. Then hopefully everyone will be equally unhappy :-) -- Posted via http://www.ruby-forum.com/.