From: Brian Candler Date: 2009-01-13T01:04:55+09:00 Subject: Re: functional programming Mike Gold wrote: > Brian Candler wrote: >> (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"). > > Ruby: > > mean = lambda { |*args| > (args.inject(0) { |acc, x| acc + x })/args.size.to_f > } > data = [[1,2,3], [4,5,6,7]] > > data.map(&mean) # fail > data.map(&:mean) # fail > data.map { |*numbers| mean.call(*numbers) } # fail > data.map { |numbers| mean.call(*numbers) } # phew, found it OK, I think I see what you're getting at. You've written a lambda with a splat (i.e. which takes a variable number of arguments), but needed a splat at the caller side too. Otherwise the lambda receives a single argument, which is an Array object. You could have written the lambda to take a single Array argument: mean = lambda { |arg| arg.inject(0) ...etc } I don't know enough about Lisp to say how Lisp resolves the same dichotomy, but surely it must exist. That is, there must be a difference between calling a function foo with a (list of) n arguments, and calling a function foo with 1 argument which is itself a list? -- Posted via http://www.ruby-forum.com/.