LISP to Ruby translation

Discussion in 'Ruby' started by Douglas Livingstone, Dec 15, 2005.

  1. Jut a quick one, how do you translate this:

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

    to Ruby?

    Cheers,
    Douglas
    Douglas Livingstone, Dec 15, 2005
    #1
    1. Advertising

  2. 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
    Joel VanderWerf, Dec 15, 2005
    #2
    1. Advertising

  3. Douglas Livingstone

    Neil Stevens Guest

    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 -

    'A republic, if you can keep it.' -- Benjamin Franklin
    Neil Stevens, Dec 15, 2005
    #3
  4. --sm4nu43k4a2Rpi4c
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    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:
    > >=20
    > > ((if (zero? 0) + -) 3 4)

    >=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--
    Edward Faulkner, Dec 15, 2005
    #4
  5. Stephen Waits wrote:
    >
    > On Dec 14, 2005, at 6:36 PM, Douglas Livingstone wrote:
    >
    >> ((if (zero? 0) + -) 3 4)
    >> => 7
    >>
    >> to Ruby?

    >
    >
    > 7 if true
    >
    > --Steve
    >


    7

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Dec 16, 2005
    #5
  6. Joel VanderWerf <> wrote:
    > Stephen Waits wrote:
    > >
    > > On Dec 14, 2005, at 6:36 PM, Douglas Livingstone wrote:
    > >
    > >> ((if (zero? 0) + -) 3 4)
    > >> => 7
    > >>
    > >> to Ruby?

    > >
    > >
    > > 7 if true
    > >
    > > --Steve

    >
    > 7


    There - who says ruby isn't concise!

    martin
    Martin DeMello, Dec 16, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ekzept
    Replies:
    0
    Views:
    354
    ekzept
    Aug 10, 2007
  2. nanothermite911fbibustards
    Replies:
    0
    Views:
    362
    nanothermite911fbibustards
    Jun 16, 2010
  3. nanothermite911fbibustards
    Replies:
    0
    Views:
    307
    nanothermite911fbibustards
    Jun 16, 2010
  4. Mark Wilson

    Ruby Tk documentation translation

    Mark Wilson, Aug 18, 2003, in forum: Ruby
    Replies:
    0
    Views:
    106
    Mark Wilson
    Aug 18, 2003
  5. Replies:
    4
    Views:
    175
    Erik Hollensbe
    May 2, 2006
Loading...

Share This Page