[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LFM + LFSP = LFE?



[Steven Grady <grady@xcf.berkeley.edu>]
> > >>>True of Ruby, too. Instead of:
> > >>>(map (lambda (n) (* n 2)) '(0 1 2 3 4))
> > >>>You write:
> > >>>[0,1,2,3,4].map{|n| n*2}
> > >>>
> > >>What's the Ruby equivalent of this?
> > >>
> > >>(map * '(1 2 3) '(4 5 6))
> > Python:
> > 
> > map(mul, [1,2,3], [1,2,3])
> > 
> > Mind you, I think this example shows more about the flexibility of 
> > Scheme's map function (which Python's happens to share) than about syntax.
> 
> Actually, I think the example does show something about syntax/semantics -- I
> don't believe there's an obvious, natural version of this for Ruby.  Ruby's
> approach to iteration is fundamental to the language, and to the idiom of its
> programs.  It uses a simple OO style (one receiver, no language-supported
> multimethods), rather than a functional style.  Note that the Ruby
> implementation of "map" is a method on Enumerable objects.  
> 
> That being said, it is certainly possible to define a non-OO map function
> (which would actually be a method in Kernel):
>     map([1, 2, 3], [1, 2, 3]) {|x,y| x*y}
> But it seems somewhat unnatural in Ruby.
> 

A more natural solution might be to conceive of map and friends as
operations on function objects instead, so:

 {|x,y| x*y}.map([1, 2, 3], [1, 2, 3])

but the issue is, which one is more natural, in what context and to
whom? If they're all equally valid and natural, how can the programmer
enforce that map should mean "the same thing" in each case. Basically,
in a message-passing language, how does one ensure that an operator
commutes?

This whole problem is pretty much the main thing about message passing
that has always made me uncomfortable. For many operations, there
doesn't seem to be a single right "owner"... I don't, for instance,
basically conceive of "1 + 2" as "ask 1 to add 2 to itself." I've yet
to see a compelling solution to this problem in a message-passing OO
language (I don't consider C++/Java-style "math ops are magic global
functions" compelling).

Matt

-- 
Matt Hellige                  matt@immute.net
http://matt.immute.net