········@gold.com.br (Wladimir K�mmer de Paula) writes:
> How can i read the second atom in a list
Did you really mean "atom", or are you just looking for the SECOND
function? BTW, note that excellent Lisp references are available
online, including Ken Pittman's hypertext version of the complete ANSI
spec. See <http://www.apl.jhu.edu/~hall/lisp.html>.
- Marty
In article <················@snews2.zippo.com>,
Wladimir K�mmer de Paula <········@gold.com.br> wrote:
>Hi folks,
>
>i'm a new Lisp-addict
>How can i read the second atom in a list
If you mean that literally, and if you count only atoms at the top
level of the list, then you could use the function:
(defun read-nth-atom (list n)
"Read nth top-level atom in list, if found"
(cond
((null list) nil)
((not (atom (first list))) (read-nth-atom (rest list) n))
((eql n 1) (setf (first list) (read)))
(t (read-nth-atom (rest list) (1- n)))))
Here is a sample dialogue:
? (read-nth-atom '(a (this is not an atom) b c) 2)
testing
TESTING
? +
(READ-NTH-ATOM '(A (THIS IS NOT AN ATOM) TESTING C) 2)
Sure enough, the second top-level atom in the first argument to
read-nth-atom has been "read". :-)
--
Dave Seaman ·······@purdue.edu
++++ stop the execution of Mumia Abu-Jamal ++++
++++ if you agree copy these lines to your sig ++++
++++ see http://www.xs4all.nl/~tank/spg-l/sigaction.htm ++++
········@gold.com.br (Wladimir K�mmer de Paula) writes:
>i'm a new Lisp-addict
>How can i read the second atom in a list
How can you be sure it's an atom (and not a cons cell, an array or
some other compositive data type)?
Wladimir K�mmer de Paula wrote:
>
> Hi folks,
>
> i'm a new Lisp-addict
> How can i read the second atom in a list
>
> Wladimir
*If* you mean:
(get-second-atom '(a b))
and:
(get-second-atom '(() a (b c)))
should both return 'b, you are in for some fun. :)
I will wait for confirmation of my interpretation of your spec before
going much further, but the way I have handled this sort of thing is to
write a generic traversal function which accepts as one parameter a
function to be applied to each atom in a tree.
By "atom" I take you to mean non-list, and let's assume all lists are
proper lists.
Then you create a closure that keeps count of the "atoms" found and uses
#'throw to return the second (or nth in the general case) atom when the
count gets to the point you want (two in your case), and finally use
#'catch around an invocation of the traversal function to which you pass
your target list and the value 2.
Lemme know if that is what you want and I'll get more specific. BTW,
Graham's "On Lisp" is good on closures.
Isn't Lisp great! <G> Welcome to the fan club.
Ken
: i'm a new Lisp-addict
: How can i read the second atom in a list
If you mean "get" instead of "read" (read has a special meaning in
lisp), than you're looking for cadr, as:
(setq l '(a s d f))
(cadr l)
=> S
or, equivalently: second
(second l)
=> S
or, eq...: nth 1
(nth 1 l)
=> S
But at this rate it's gonna take you a while to get high. For a quick
trip, try to figure out why the following does the same as the
experssions above, although a LOT less efficiently:
(do ((n (reverse l) (cdr n))) ((null (cddr n)) (car n)))
=> S
Cheers,
'Jeff
········@gold.com.br (Wladimir K�mmer de Paula) wrote:
>Hi folks,
>i'm a new Lisp-addict
>How can i read the second atom in a list
If the list is x, then it's just
(second (remove-if-not #'atom x))
Of course, there are more efficient ways of doing the same thing.
>Wladimir
Le Hibou http://homepages.enterprise.net/hibou/
There are a number of mechanical devices which increase sexual
arousal, particularly in women. Chief among them is the
Mercedes-Benz 380SL convertible. -- PJ O'Rourke.