LISP to Ruby translation

  • Thread starter Douglas Livingstone
  • Start date
D

Douglas Livingstone

Jut a quick one, how do you translate this:

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

to Ruby?

Cheers,
Douglas
 
J

Joel VanderWerf

Douglas said:
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)
 
N

Neil Stevens

Douglas said:
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)
 
E

Edward Faulkner

--sm4nu43k4a2Rpi4c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
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

--sm4nu43k4a2Rpi4c
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDoe/gnhUz11p9MSARAowLAJ9qqzeqw4XH5gM5Q5qVwxBAe6ZcewCfR0mJ
MgzAEU+72YnEkfTm+cvrKno=
=+lDL
-----END PGP SIGNATURE-----

--sm4nu43k4a2Rpi4c--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top