From: patrick
Subject: school assign due in 2 days, braindead
Date: 
Message-ID: <82nhbh$868$1@autumn.news.rcn.net>
how do you write a functional to detect for a numeric palindrome given a
positive integer, and if it isn't, add the number to its reverse, and
add that to its reverse, ... until you get a palindrome?  only
primitives like atom, null, appendl, appendr, length, head, tail, first,
last, and functionals like apply-to-all, reverse, funcall, mapcar... may
be used.  boy i'm confused, but homework is due in 2 days, have been
learning functionals for only a week, still no clue, beginning to hate
functional programming.  code please, lisp gurus!

From: Lieven Marchand
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <0146ce5a.daf646b8@usw-ex0102-011.remarq.com>
In article <············@autumn.news.rcn.net>, "patrick"
<··@nospam.com> wrote:
> how do you write a functional to detect for a numeric palindrome
> given a
> positive integer, and if it isn't, add the number to its reverse,
> and
> add that to its reverse, ... until you get a palindrome?  only
> primitives like atom, null, appendl, appendr, length, head, tail,
> first,
> last, and functionals like apply-to-all, reverse, funcall,
> mapcar... may
> be used.  boy i'm confused, but homework is due in 2 days, have
> been
> learning functionals for only a week, still no clue, beginning to
> hate
> functional programming.  code please, lisp gurus!

We live to serve.

(defun palindrome-p (number)
  (let* ((s (format nil "~A" number))
         (r (reverse s)))
     (string-equal r s)))

(defun add-reverse (number)
  (let* ((s (format nil "~A" number))
         (r (reverse s))
         (c (format nil "#.(+ ~A ~A)" s r)))
     (read-from-string c)))

(defun make-palindrome (number)
  (loop until (palindrome-p number)
        do
       (setf number (add-reverse number)))
  number)

Making it a bit more functional is left as an exercise for the reader


* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!
From: patrick
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <82ou2d$h9n$1@autumn.news.rcn.net>
>(defun palindrome-p (number)
>  (let* ((s (format nil "~A" number))
>         (r (reverse s)))
>     (string-equal r s)))
>
>(defun add-reverse (number)
>  (let* ((s (format nil "~A" number))
>         (r (reverse s))
>         (c (format nil "#.(+ ~A ~A)" s r)))
>     (read-from-string c)))
>
>(defun make-palindrome (number)
>  (loop until (palindrome-p number)
>        do
>       (setf number (add-reverse number)))
>  number)
>
>Making it a bit more functional is left as an exercise for the reader


we need to implement addition ourselves.
something like:

  1384
+ 4831
--------
5 11 11  5

how do we implement something that would account for carry's, and make
it
into a list that looks like:

5215

all the processing must be done as pure list operations, mind boggling!
From: Barry Margolin
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <BeT34.22$c72.933@burlma1-snr2>
In article <············@autumn.news.rcn.net>, patrick <··@nospam.com> wrote:
>we need to implement addition ourselves.
>something like:
>
>  1384
>+ 4831
>--------
>5 11 11  5
>
>how do we implement something that would account for carry's, and make
>it
>into a list that looks like:
>
>5215
>
>all the processing must be done as pure list operations, mind boggling!

Think out the exact process you do when adding numbers on paper, and then
program it.

You can implement the number as a list of digits; it would probably
simplify things if you reverse it (i.e. the units digit should be the first
element of the list, the 10's digit next, and so on), since addition is
usually done by starting from the units digit and working your way up, and
Lisp makes it easiest to traverse lists from the front.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: ········@hotmail.com
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <82pr6k$34v$1@nnrp1.deja.com>
patrick wrote:
> >we need to implement addition ourselves.
> >something like:
> >
> >  1384
> >+ 4831
> >--------
> >5 11 11  5
> >
> >how do we implement something that would account for carry's, and
make
> >it
> >into a list that looks like:
> >
> >5215
   ^^^^
> >
> >all the processing must be done as pure list operations, mind
boggling!
>
<cruel>
Why in the world would you want (+ 1384 4831) to be equal to 5215, or,
in your case, '(5 2 1 5)?  Last time I checked (+ 1384 4831) => 6215.
How late in the night/early morning did you write this post?
</cruel>
<kind>
(defmethod /10 ((x integer))
  (floor (/ x 10)))
(defmethod +units ((x integer) (y integer))
  (/10 (+ (mod x 10) (mod y 10))))

Barry Margolin wrote:
> You can implement the number as a list of digits; it would probably
> simplify things if you reverse it (i.e. the units digit should be the
first
> element of the list, the 10's digit next, and so on), since addition
is
> usually done by starting from the units digit and working your way
up, and
> Lisp makes it easiest to traverse lists from the front.
>

;; per Barry, above:
(defmethod list+ ((x integer) (y integer))
  (if (and (zerop x) (zerop y))
    ()
    (multiple-value-bind (carry result) (+units x y)
	(cons (* 10 result) (list+ (/10 x) (+ carry (/10 y)))))))

; (list+ 1384 4831) => (5 1 2 6) ;; note: result in reverse order

</kind>

So, you've got 95% of your assignment done for you.  Now, all you need
to do write a fn that converts a list (numbers in reverse order) to an
integer (hint:  one if fn with recursion will do the job ...
challenge:  I wrote it in 45 characters, can you match that?) for
further comparisons, and handle non-integers.  Piece o' cake!

Douglas M. Auclair

P.S. does your prof read comp.lang.lisp?
P.P.S. did you learn the values fn and CLOS in your class?



Sent via Deja.com http://www.deja.com/
Before you buy.
From: fluffy
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <38508299.CD25B0FA@fluffy.fluffy>
patrick wrote:
> 
> how do you write a functional to detect for a numeric palindrome given a
> positive integer, and if it isn't, add the number to its reverse, and
> add that to its reverse, ... until you get a palindrome?

(defun numrev (n)
    (parse-integer (reverse (format nil "~a" n))))

(defun numpalp (n)
    (let ((s (format nil "~a" n)))
      (string= s (reverse s))))

(defun doit (n)
    (do ((p n (+ p (numrev p))))
        ((numpalp p) p)))


seems to work... I'm sure it could be done better.

> only primitives like atom, null, appendl, appendr, length, head, tail, first,
> last, and functionals like apply-to-all, reverse, funcall, mapcar... may
> be used.


why???

that seems about as meaningful as manipulating arrays with nothing but arithmetic functions. What
are you supposed to learn from this bondage exercise, anyway? Inappropriate programming methods, probably.

btw, will this terminate for all integers? I'm still waiting for 3487328934423245
From: Barry Margolin
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <Pga44.46$c72.3282@burlma1-snr2>
In article <·················@fluffy.fluffy>,
fluffy  <······@fluffy.fluffy> wrote:
>> only primitives like atom, null, appendl, appendr, length, head, tail, first,
>> last, and functionals like apply-to-all, reverse, funcall, mapcar... may
>> be used.
>
>
>why???
>
>that seems about as meaningful as manipulating arrays with nothing but
>arithmetic functions. What
>are you supposed to learn from this bondage exercise, anyway?
>Inappropriate programming methods, probably.

It seems more like an exercise in numerical algorithms, using Lisp as the
implementation language.  Common Lisp has lots of convenience functions
that make it a snap to implement the goal, but you don't really learn
anything about numbers and data structures by using them.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Barry Margolin
Subject: Re: school assign due in 2 days, braindead
Date: 
Message-ID: <oea44.45$c72.3282@burlma1-snr2>
In article <············@nnrp1.deja.com>,  <········@hotmail.com> wrote:
>patrick wrote:
>(defmethod /10 ((x integer))
>  (floor (/ x 10)))

If you wrote this as:

(defmethod /10 ((x integer))
  (floor x 10))

then you wouldn't need to multiply by 10 in LIST+.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.