
How Ruby Borrowed a Decades Old Idea From Lisp - pat_shaughnessy
http://patshaughnessy.net/2012/9/18/how-ruby-borrowed-a-decades-old-idea-from-lisp
======
judofyr
Sure, lambdas are not new, but there's still some interesting (maybe not worth
calling them novel) ideas in Ruby's blocks:

First of all the observation that most method only care about a single block.
By having custom syntax for "method that takes a single block" you get a
certain simplicity that makes methods look like built-in keywords:

    
    
        loop { puts "Nuts" }
    

It does make multi-block methods look ugly, but frankly, I think they look
ugly in any language.

Lisp has macros (which has both it's advantages and disadvantages), but I find
that Ruby's blocks make the lack of macros more tolerable.

Compared to JavaScript/Perl/Python-lambas, Ruby's blocks don't introduce
another scope. "self" and "return" still refer to the method's scope (I'm not
sure what this type of scope is called; it's not the variable scope I'm
talking about here). By introducing another set of keywords that work on the
block ("next" and "break") you can easily wrap code in blocks without changing
the semantics. See <http://yehudakatz.com/2012/01/10/javascript-needs-blocks/>
for more information about the difference between blocks and lambdas in
object-oriented languages.

~~~
snogglethorpe
Blocks, and the way they make it very easy to add new control structures
"seamlessly", are indeed cool—but aren't ruby's blocks taken pretty much
verbatim from smalltalk (which of course is also many decades old, if not
quite as old as lisp)?

Indeed, smalltalk seems a better example, as it uses methods and blocks to
implement _all_ control structures, even the "built-in" ones.

~~~
pat_shaughnessy
I admittedly don't know a lot about Smalltalk - I was aware Ruby's (and every
other language's) object oriented ideas were invented originally with
Smalltalk, but I didn't know it had a block/closure syntax also.

In this post I wanted to go back as far in time as I could :)

~~~
vidarh
> In this post I wanted to go back as far in time as I could :)

Then you might want to look at Simula as well. As much as the term "object
oriented" was invented in conjunction with Smalltalk, a lot of the basic
concepts (classes, objects...) came from Simula (but not closures).

------
semiquaver
> older languages such as C or Java.

Ruby and Java saw their first public releases within a month of each other.

~~~
pat_shaughnessy
Yes you're right! I stand corrected; will update the post when I have a
chance.

------
npsimons
"It's [2012], and programming languages have almost caught up with 1958." --
paraphrased from, oh, you know, some guy :)

~~~
JoeAltmaier
DOn't imagine the reasons languages are different from List is that nobody
knew any better.

Programming in Lisp is like programming in assembler. Reading it is worse. The
learning curve is tremendous.

Languages serve lots of demographics, not just comp-sci wonks.

~~~
KirinDave
When did the shift that makes this okay happen? Once upon a time, people who
knew a lot about computer science were respected and praised; people tried to
emulate their algorithms, coding style and planning.

Now, they are "wonks" for knowing a slightly different (and demonstrably
effective) style of programming.

~~~
JoeAltmaier
Hey! No offense meant. Its just true that a lot of programmers are not CS-
trained. So not the target demographic of Lisp.

~~~
Jtsummers
I would contend that not being CS-trained doesn't eliminate them from the pool
of people who could benefit from knowing Lisp and similar languages. As a
programmer, unless you just want to be outclassed in a few years, you have to
stay on top of developments in the industry and the history of the industry.
Knowing the languages that modern languages pull their tricks from is one such
way. And it doesn't take a CS degree to do that with many of the resources
being put out these days.

~~~
dalke
If someone is a biology researcher who does some programming for their
research, does that programmer need to stay on top of developments in the
software industry? Or know the history of the programming field ... as well as
keep up with the biology field?

~~~
Lio
Well I guess that depends on what you're doing within you're research.

In bioinformatics knowing about, say, functional programming and parallel
algorithms could be really useful where data coming out of sequencers is
growing faster than Moore's law can keep up, if you want to scale across many
cores as efficiently as possible.

Or graph theory, or... any number of useful techniques waiting for an
application.

~~~
dalke
Certainly. But most aren't so inclined. Most need a way to glue together
existing components. For example, "make a web connection to X, call command-
line program Y, and use a shared-library extension to do Z. Plot in Excel."
The result might be very useful scientifically, and it's definitely
programming, but there's no real reason to learn about lambdas, s-expressions,
and monads.

------
ericbb
I've been thinking about block implementation off and on for a while (hobbyist
language designer here) and I'd like to try implementing them so that they are
never values in the language semantics.

That way, a class of errors becomes impossible; namely, those errors that
involve returning from a block whose method has already returned. I would have
closures in the language but they would be just like Scheme closures: they
would be independent of their creation context except for lexical bindings. I
would compile blocks right into the method in which they appear.

Unfortunately, I don't have experience with any language that uses blocks. I'm
quite familiar with closures but not blocks. I'm wondering if any Ruby or
Smalltalk (or ...) programmer can give some reasons why blocks ought to be
values.

Also, (referring to the post) does anyone know why rb_block_t was not embedded
in rb_control_frame_t? That's the usual idiom in C and I can't think of a
reason not to use it.

------
djacobs

      In fact, the computer science concept behind blocks, called “closures”
    

As far as I know, the language-free concept of "block" is orthogonal to the
concept of scope. That is, a block is not by _necessity_ a closure.

~~~
pat_shaughnessy
You might be right about the general meaning of the term "block." However, in
Ruby blocks happen to be implemented as closures. This was one of the main
points I was trying to make... that blocks aren't as simple as they seem, at
least in Ruby.

~~~
djacobs
That's a totally legitimate point. I'm just pointing out that the quoted bit
would lead the reader to believe that a block is defined as a closure.

The problem is almost everyone I talk to in the startup scene thinks that an
anonymous function/block == a closure.

~~~
chimeracoder
> The problem is almost everyone I talk to in the startup scene thinks that an
> anonymous function/block == a closure.

This is a big pet peeve of mine - I keep seeing people use 'closure' as a
synonym for 'lambda'. It's a simple error, but it bugs the hell out of me.

Then again, it's a dead giveaway that the person has never programmed in a
functional paradigm, which may be a good thing to know, depending on the
context.

------
twelvechairs
I'd be interested if the author had any thoughts on the ruby_parser project's
unresolved issues of S-expression use in ruby 1.9, as at
[http://blog.zenspider.com/blog/2012/08/what-are-block-
args-....](http://blog.zenspider.com/blog/2012/08/what-are-block-args-.html)
(not my project, but something I'd like to use once they've solved all this -
I know the author of the above link was looking for input).

~~~
pat_shaughnessy
I'll be sure to take a look at that, and thanks for the link. No, no thoughts
on it right now.

------
VeejayRampay
Excellent article, I really enjoy reading about the nuts and bolts of
languages.

~~~
pat_shaughnessy
Thank you!

------
andrewcooke
i thought old (original) lisp was dynamically scoped?

~~~
philwelch
Yup--I believe it was Scheme that introduced lexical scoping to Lisp, with
Common Lisp adopting it later on. Of the commonly used Lisps today, only Emacs
Lisp is dynamically scoped.

~~~
andrewcooke
so the article is misleading, isn't it? it says "Lisp programming language,
invented in 1958 by John McCarthy. Lisp pioneered many fundamental computer
science concepts, including closures", but closures were first created
elsewhere - see
[http://en.wikipedia.org/wiki/Closure_(computer_science)#Hist...](http://en.wikipedia.org/wiki/Closure_\(computer_science\)#History_and_etymology)

~~~
pat_shaughnessy
Oops - I missed that in my research. Yes, I agree this sentence is misleading;
I'll update the text later on today. I suppose it's only true that Lisp
(specifically Scheme) was the first widely used implementation of closures.

~~~
andrewcooke
yes, i think that would be a better way of phrasing it. it's weird how old
scheme is (1975 - 37 years old).

