12:53 pm, 6 Jan 06
ruby blocks
A friend has been learning Ruby and asking questions. His first: why are procs and blocks different? (More simply: why aren't blocks special syntax for procs?)
All I could come up with is that "return" in a block returns from the lexically enclosing function, while "return" in a proc returns from the proc itself. (This is a useful distinction, though it leads to the confusing LocalJumpError exception when you call a block after control has passed away from its lexically enclosing function...)
Is that it?
All I could come up with is that "return" in a block returns from the lexically enclosing function, while "return" in a proc returns from the proc itself. (This is a useful distinction, though it leads to the confusing LocalJumpError exception when you call a block after control has passed away from its lexically enclosing function...)
Is that it?
Is a proc any different than a block with an anonymous enclosing function?
I'm really curious about this now...
But they're really the same thing inside:
This is interesting because I was thinking earlier about this very problem for the hypothetical “perfect programming language” I've been working on. In the end though, I distracted myself with the realisation that my syntax doesn't permit anonymous functions to look like blocks because the parameters need to be named somehow. Now that you've mentioned this, I'll have to look at Ruby and see how it deals with this.
When I talk about it, I normally qualify it as “My perfect programming language”, so that others aren't confused into thinking that I mean “The perfect programming language”, which is of course a fallacy. Ruby can't be my perfect programming language because it doesn't have enough punctuation. ;)
Briefly, an anonymous function looks like
(\x. x + 1)
(this returns whether its argument is greater than one; the backslash is an attempt at producing a lambda). So you can writemap (\x. x + 1) mylist
but you can write it more simply as
map (+ 1) mylist
because + is a two-argument function and you've specified one of the arguments.
There's more on this here:
http://haskell.org/hawiki/PointFreeStyle
They also tend to do weird things like bunch up ops in typesetting, which looks bizarre in arrows papers. (As if arrows papers were lacking in bizarritude already.)