Re: Hints on recursion
- From: "mpeever" <mpeever@xxxxxxxxx>
- Date: 29 Nov 2005 12:01:43 -0800
OK, so I may be breaking netiquette, posting when I've not been here a
day, but I had to throw this out.
I'm a newbie, my Lisp sucks. But I did come up with what i think might
be a valid solution, although I did use cons:
1. return true when the element appears in the first two places in the
list
2. return false if the list has fewer than 2 elements
3. delete any non-matching elements from the list and recurse
My version looks like this, but someone who actually knows what they're
doing could do it better, I'm sure:
(defun find-twice (a lst)
(cond ((and (eq a (first lst)) (eq a (first (rest lst)))) lst)
((or (eq (first lst) nil) (eq (rest lst) nil)) nil)
((not (eq (first lst) a)) (find-twice a (rest lst)))
(t (find-twice a ((first lst) . (cdr (rest lst)))))))
- mpeever
.
- Follow-Ups:
- Re: Hints on recursion
- From: Lars Brinkhoff
- Re: Hints on recursion
- From: Pascal Bourguignon
- Re: Hints on recursion
- References:
- Hints on recursion
- From: zion_zii
- Hints on recursion
- Prev by Date: Re: Another conversion to C or Java...
- Next by Date: Re: Hints on recursion
- Previous by thread: Re: Hints on recursion
- Next by thread: Re: Hints on recursion
- Index(es):
Relevant Pages
|
|