Forum: Ruby LISP to Ruby translation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2005-12-15 03:36
(Received via mailing list)
Jut a quick one, how do you translate this:

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

to Ruby?

Cheers,
Douglas
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2005-12-15 03:46
(Received via mailing list)
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)
3c155ef399326d533efc2eb91ac992e5?d=identicon&s=25 Neil Stevens (Guest)
on 2005-12-15 03:49
(Received via mailing list)
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
Ec2a9a9da5c8a5f14a0fe2361ae4e08a?d=identicon&s=25 Mark J.Reed (Guest)
on 2005-12-15 23:24
(Received via mailing list)
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)...
036a1b88dafaab8ffd73a8b0a74b5b38?d=identicon&s=25 Edward Faulkner (Guest)
on 2005-12-15 23:40
(Received via mailing list)
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
280b41a88665fd8c699e83a9a25ef949?d=identicon&s=25 Stephen Waits (Guest)
on 2005-12-16 08:12
(Received via mailing list)
On Dec 14, 2005, at 6:36 PM, Douglas Livingstone wrote:

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

7 if true

--Steve
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2005-12-16 08:27
(Received via mailing list)
Stephen Waits wrote:
>
> --Steve
>

7
2cf6d8e639314abd751f83a72e9a2ac5?d=identicon&s=25 Martin DeMello (Guest)
on 2005-12-16 11:04
(Received via mailing list)
Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> > 7 if true
> >
> > --Steve
>
> 7

There - who says ruby isn't concise!

martin
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2005-12-17 00:31
(Received via mailing list)
Edward Faulkner <ef@alum.mit.edu> writes:

> I think in CL the equivalent would be:
>
> ((if (zerop 0) #'+ #'-) 3 4)

No, you need:

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

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

And that is why. :-)
This topic is locked and can not be replied to.