[Haskell-beginners] Haskell as a useful practical 'tool' for intelligent non-programmers
Mike Meyer
mwm at mired.org
Sun Apr 29 06:15:42 CEST 2012
On Sat, 28 Apr 2012 22:22:34 -0400
Michael Orlitzky <michael at orlitzky.com> wrote:
> On 04/28/2012 08:47 PM, Mike Meyer wrote:
> > Ruby makes a bad fit if Haskell is a goal (and that's a good
> > goal). Ruby functions aren't first-class objects, and can't simply be
> > passed to other functions as arguments.
>
> That's like, the least true thing you can say about Ruby =)
I respectfully disagree. For instance, I could have said that it uses
indentation to delimit blocks like ABC does. That's much less true.
But I believe the statement about functions not being first class
objects is true. Of course, I don't use ruby on a regular basis
because that turns out to be the case every time I go look at it. If
the language has changed so this can be done in a manner that's as
straightforward as Python, I'd be interested in hearing about it.
For the record, the Python version of your example is:
def foo():
print("foo")
call_arg = apply
call_arg(foo)
> A simple test program:
>
> $ cat fcf.rb
> def foo
> puts "foo"
> end
>
> def call_arg(f)
> f
> end
>
> call_arg(foo)
And that would do it, except, as Lyndon explained, it's not doing the
right thing. You provided one of the workarounds (using Proc) when you
corrected it.
> Moreover, every function and method implicitly accepts a function as an
> argument:
Doesn't quite seem that way to me.
> $ cat yield.rb
> def call_block
> yield
> end
>
> call_block { puts "foo" }
>
> This allows you some nice do-block syntactic sugar instead of lambdas
> which can get ugly.
They are accepting a *block* as an argument. And you're right - blocks
are cleaner than lambdas. It's also more powerful than Python's
lambdas, which are restricted to being expressions. A lot of people
(me among them) would like Python to have something like Ruby's block
construct for anonymous code, and there are regular proposals on how
to do it (including from me). If someone ever comes up with a way
that both reads well in an indent-delimited language and doesn't
invite incredible abuse, it'll probably make it into the language.
But that doesn't make a function a first class object. It doesn't even
make a block a first class object (can I give a block a name?).
There are other ways to work around this limitation (I think I found 6
last time I looked). IIRC, the Proc version was about the cleanest.
Note that I *didn't* say that this limitation makes Ruby a bad
language. I don't like it, because I find that languages that don't
make functions first-class objects grate on me. If it doesn't bother
you, you're a better person than I am.
That's why I added the proviso "if Haskell is a goal". Trying to go
from a language where functions can't be treated as first class
objects, requiring wrapper objects and similar things in order to
write in a functional style to one where a functional style is de
rigueur just seems like a bad idea.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
More information about the Beginners
mailing list