From: pjb@... (Pascal J. Bourguignon) Date: 2009-01-11T07:10:12+09:00 Subject: Re: functional programming Brian Candler writes: > 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))) This is Scheme, not Lisp. Scheme is a lisp-1, not a lisp-2. In Scheme, variables and functions share the same namespace. But this is not the case of Common Lisp and neither of Ruby. > 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'). Ruby doesn't work like that. irb(main):033:0> plus4 = lambda { |x| x + 4} plus4 = lambda { |x| x + 4} # irb(main):034:0> (plus4 2) (plus4 2) NoMethodError: undefined method `plus4' for main:Object from (irb):34 irb(main):035:0> > But we could be talking at Scheme/Common Lisp cross-purposes here. Indeed. > 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. Note that there is a sizeable group of lisp programmers who don't use emacs, but vim or other IDEs. http://cybertiggyr.com/15-vim/ http://www.cliki.net/vim >>> 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. Anyways, this part of the discussions raised to justify my papering over these distinctions, and to have an homogeneous treatment of methods, 'functions' and lambdas when we want to do functional programming in Ruby. Indeed, there are distinctions made by Ruby. Indeed, for functional programming these distinctions are an hindrance both syntactically and semantically, hence the definitions I gave. > [...] > 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 :-) LISP, yes. But since that time, we standardized, and now there's one true Lisp: Common Lisp. And you can easily write programs portable across different implementations of Common Lisp. And see above, you can write lisp in any editor, even vim. -- __Pascal Bourguignon__