tags:

views:

82

answers:

2

How to use nth function in lisp if my my variable is combination of list and cons-cell for eg:
(setq aa '(1 2) )
(nconc aa (+ 1 2))
this return me (1 2 . 3)
when i say (nth 1 aa) it returns 2
but when i use (nth 2 aa ) it throws error

+5  A: 

NTH returns the car of the nth (0, 1, 2 ...) cons cell.

Since your second cdr is not a cons cell it is an error to get the car of it.

(nthcdr 2 '(1 2 . 3))   returns 3

(last '(1 2 . 3))  returns (2 . 3)

Usually it is a good idea to avoid improper lists, where the cdr of some cell is not a cons or NIL.

Rainer Joswig
A: 

Yeah. Another way to say it is: "don't do that, then".

offby1