[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

LISP to Ruby translation

Douglas Livingstone

12/15/2005 2:36:00 AM

Jut a quick one, how do you translate this:

((if (zero? 0) + -) 3 4)
=> 7

to Ruby?

Cheers,
Douglas


4 Answers

Joel VanderWerf

12/15/2005 2:43:00 AM

0

Douglas Livingstone wrote:
> Jut a quick one, how do you translate this:
>
> ((if (zero? 0) + -) 3 4)
> => 7
>
> to Ruby?
>
> Cheers,
> Douglas
>

3.send(x.zero? ? :+ : :-, 4)

It's a little hard to read with all those colons. Maybe this is better:

3.send(if x.zero? then :+ else :- end, 4)

Or you could replace symbols with strings for readability, at a cost to
speed:

p 3.send(x.zero? ? "+" : "-", 4)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Neil Stevens

12/15/2005 2:47:00 AM

0

Douglas Livingstone wrote:
> Jut a quick one, how do you translate this:
>
> ((if (zero? 0) + -) 3 4)
> => 7
>
> to Ruby?

Well, these kind of questions are rarely helpful; translating an
idiomatic expression out of context from one language is often useless,
because in a greater context the ruby code might be doing something
completely different to begin with.

But, if you want a one-liner...

3.method(0.zero? ? :+ : :-).call(4)

--
Neil Stevens - neil@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

Mark J. Reed

12/15/2005 10:22:00 PM

0

Douglas Livingstone wrote:
> Just a quick one, how do you translate this:
>
> ((if (zero? 0) + -) 3 4)

What dialect of LISP is that? Not Common... CL has no "zero?"
(it's "zerop"), and + and - refer to input history rather than the functions
associated with the operators. To do something like that in CL I think
you have to use the characters '+ and '- and an (eval)...

Edward Faulkner

12/15/2005 10:38:00 PM

0

On Fri, Dec 16, 2005 at 07:22:39AM +0900, Mark J.Reed wrote:
> Douglas Livingstone wrote:
> > Just a quick one, how do you translate this:
> >
> > ((if (zero? 0) + -) 3 4)
>
> What dialect of LISP is that? Not Common... CL has no "zero?"

It's Scheme. A much nicer dialect than CL, IMHO. ;-)

I think in CL the equivalent would be:

((if (zerop 0) #'+ #'-) 3 4)

Note the ugly sharp-quotes, due to the fact that CL has separate
namespaces for values and functions.

regards,
Ed