Can someone tell me how I can take an integer, reverse its digits, and then use
it as an argument to a function which requires a numeric argument? The function
which does the reversal should be efficient as it will have to work with
arbitrarily large integers. I tried converting the integer using the pack
function (explode) and was then able to use reverse but getting the result back
to an integer has so far eluded me.
From: David Schulenburg
Subject: Re: manipulating integers as strings
Date:
Message-ID: <3np0mo$3p0@news.aero.org>
In article <··········@cnn.Princeton.EDU>, "Ira H. Fuchs" <·····@princeton.edu> writes:
|> Can someone tell me how I can take an integer, reverse its digits, and then use
|> it as an argument to a function which requires a numeric argument? The function
|> which does the reversal should be efficient as it will have to work with
|> arbitrarily large integers. I tried converting the integer using the pack
|> function (explode) and was then able to use reverse but getting the result back
|> to an integer has so far eluded me.
|>
there might be better ways to do this; but, the following will work.
[1c] USER(38): (setq n 12345)
12345
[1c] USER(39): (format nil "~a" n)
"12345"
[1c] USER(40): (nreverse *)
"54321"
[1c] USER(41): (read-from-string *)
54321
5
[1c] USER(42):
In article <··········@cnn.Princeton.EDU>, "Ira H. Fuchs" <·····@princeton.edu>
writes:
|> Can someone tell me how I can take an integer, reverse its digits, and then use
|> it as an argument to a function which requires a numeric argument? The function
|> which does the reversal should be efficient as it will have to work with
|> arbitrarily large integers. I tried converting the integer using the pack
|> function (explode) and was then able to use reverse but getting the result back
|> to an integer has so far eluded me.
In Common Lisp:
(with-input-from-string (port (reverse (format nil "~d" n))) (read port))
---Luigi