From: Ken Bloom Date: 2008-04-18T14:10:19+09:00 Subject: Re: When are lambdas needed? On Thu, 17 Apr 2008 20:50:18 -0500, zuo peng wrote: > the lambda expression came from the lisp community, it's kind of > abstraction of procedures. > in oo languages it's usually difficult to extend methods than extend > data structures. > ruby or other modern programming languages did a great job to mix them > together. well , back to the topic, imo to fully understand the power of > lambda(or abstraction), one has to look into the lisp/scheme world :) Lambda comes from lambda calculus. The fact that LISP uses them is just a happy coincidence. But let's compare LISP and Ruby for a moment. In LISP, any time you want to pass an unnamed ad-hoc function to another function, you define it like (lambda (var1 var2) (code)), so you wind up using the lambda macro a lot. You might have (mapcar #'(lambda (x) (* x x)) '(1 2 3 4)) which returns (1 4 9 16) In Ruby, you do this kind of thing a lot too. Only we have syntactic sugar that makes things a little nicer in many cases. The equivalent code to the LISP example is [1 2 3 4].map{|x| x*x} so you just used a lambda, but didn't have to type the keyword. There's one thing to know though. Kernel#proc and Kernel#lambda have slightly different semantics when it comes to the return, next, and break keywords. Kernel#lambda does things one way, and Kernel#proc or a bare block do things the other way. So really, Ruby uses lambdas a lot (maybe even more than LISP) but we just don't use the term very much, mostly because we've shortened the syntax. -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/