4

Is there reason why lambda is called lambda and proc is called proc?
As fair I remember both are anonymous functions... and that's where similarities with Lambdas ends. They are not taking 1 argument like lambdas from Haskell.

Why one is called lambda where both can be equally called lambda?
Why someone used name "proc" for naming one and "lambda" for naming second?

ps. They both are in a one class, Proc. There is method lambda? that shows if it is lambda.

Darek Nędza
  • 1,420
  • 1
  • 12
  • 19
  • 1
    I had been asked the same kind of question in one of my interview, but I couldn't answer. Today I will learn it from there. Thanks to bringing it up here. :) +1 – Arup Rakshit Jan 19 '14 at 10:54
  • Duplicate? http://stackoverflow.com/questions/7043181/ruby-lambda-vs-proc-new – Tony Hopkinson Jan 19 '14 at 10:57
  • @TonyHopkinson No it is not... I think, at-least the one you referenced.. – Arup Rakshit Jan 19 '14 at 10:58
  • 1
    It's duplicate of http://stackoverflow.com/questions/1740046/whats-the-difference-between-a-proc-and-a-lambda-in-ruby – lipanski Jan 19 '14 at 11:10
  • ``Proc.new`` and ``proc`` are equivalent, for the difference to ``lambda`` check the link I've posted. – lipanski Jan 19 '14 at 11:11
  • Hence the question mark. :) It does detail the differences between Proc and Lambda though, and to be honest why they called proc proc and lambda lambda is not usually of that much interest, so I'm at a loss. – Tony Hopkinson Jan 19 '14 at 11:11
  • @lipanski no, it's not duplicate. I wasn't asking about difference between `lambda` and `proc` (`->` and 'Proc.new`). I was asking about reason they call lambda - lambda and proc - proc. – Darek Nędza Jan 19 '14 at 11:56
  • @TonyHopkinson see above comment – Darek Nędza Jan 19 '14 at 11:57

2 Answers2

3

In ruby, lambdas are a special kind of Proc objects whose behavior mimics that of a method. In particular, it'll respect arity: if you give it the wrong number of args, it spits an error.

See this answer (to an unrelated question) for colorful examples of how they differ:

Why does Hash#select and Hash#reject pass a key to a unary block?

Replying to the comment: lamba and proc both product a Proc object. The only differences are the above-mentioned behavior when it comes to arity, and what happens with a return statement (see the related link in the question's comments).

As for why they're named that way, look no further than it being Matz' choice imo: this was the most natural. He defined what he means by that in an interview:

Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, 'I was surprised by this feature of the language, so Ruby violates the principle of least surprise.' Wait. Wait. The principle of least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. For example, I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming, it still surprises me.

http://en.wikipedia.org/wiki/Ruby_(programming_language)#Philosophy

In that light, note how a lambda behaves very much like a method, and in this respect qualifies as a (anonymous) function. Whereas a proc does not. Naming the first of these "lambda" seems like a natural choice (to me, anyway), whereas the second wouldn't due to the differences when it comes to arity and return statements.

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • 1
    It doesn't explain why lambda is lambda and why proc is proc etc. – Darek Nędza Jan 19 '14 at 11:58
  • Not convinced that that quote proves anything very much, beyond the Humpty Dumpty reason: “When I use a word, it means just what I choose it to mean — neither more nor less.” – Donal Fellows Jan 19 '14 at 12:18
  • @DonalFellows: if you've a better quote from Matz, please add it as an answer. Because insofar as I'm aware, lambda vs proc as a nomenclature is just as arbitrary as having decided to use e.g. Decimal or FixNum. And yes: the humpty dumpty quote is just as apt as a reply to OP: there *is* no reason beyond whatever random thoughts were in Matz' head that day. – Denis de Bernardy Jan 19 '14 at 12:23
  • @DonalFellows: I did my best to add what *might* feel natural about the convention. – Denis de Bernardy Jan 19 '14 at 12:28
  • @CarySwoveland: haha! Very illustrative way to put it. :-) – Denis de Bernardy Jan 19 '14 at 19:35
  • 2
    Denis, are you saying a lambda is (l)ike (a) (m)ethod, (b)oth (d)emand (a)rity? – Cary Swoveland Jan 19 '14 at 19:36
  • What qualify both, `lambda`/`->` and `proc`/`Proc.new`, to be an instances of class `Proc`? – Darek Nędza Jan 20 '14 at 23:11
0

Is there reason why lambda is called lambda and proc is called proc?

Let's see what happened with call to proc, and lambda in ruby 1.8.7:

aa = lambda {|a| nil }
# => #<Proc:0xb7351850@(irb):6>
aa.call
# warning: multiple values for a block parameter (0 for 1)
# => nil 

aa = proc {|a| nil }
# => #<Proc:0xb73451cc@(irb):10> 
aa.call
# warning: multiple values for a block parameter (0 for 1)

As we can see, there is no difference between the proc, and lambda in Ruby. I believe that the proc had appeared in Ruby at first. Because it just alias to Proc.new, which creates the Proc object. Then the lambda had added into ruby, because the Lambda is the so called anonymous function in computer programming area, and the developers could see in the language well-known name for that fucntion class.

Let's see that happened with the operators in ruby 1.9.1:

aa = lambda {|a| nil }
# => #<Proc:0x8054340@(irb):1 (lambda)> 
aa.call
# ArgumentError: wrong number of arguments (0 for 1)
from (irb):2:in `call'
from (irb):2
from /home/malo/.rvm/rubies/ruby-1.9.1-p431/bin/irb:12:in `<main>'
aa = proc {|a| nil }
# => #<Proc:0x8319bf0@(irb):3> 
aa.call
# => nil 

As we can see, in ruby 1.9.2 (I guess since ruby 1.9) was added attribute lambda into Proc instance saying that the anonymouns function will not accept the wrong argument amount. So passing the 0 arguments into 1 required raises the ArgumentError exception. While the passing the 0 arguments into 1 required for the proc object silently drops the unnecessary argument.

As you know from ruby 1.9.1 was added -> as an alias to argumentless lambda operator:

 aa = -> { nil }
 # => #<Proc:0x8056ffc@(irb):1 (lambda)> 
 aa = -> {|a| nil }
 # SyntaxError: (irb):2: syntax error, unexpected '|'
     aa = -> {|a| nil }
              ^
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69