This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

nth element of a lisp

6 posts · 15106 views

How to define a function that takes a list and n, and returns the nth element of the list, using cdr and car.

Re: nth element of a lisp

Homework?

CDR returns a list without the first element, right? Calling CDR on the output of CDR returns a list without the first two elements; the third element is at the front of the resulting list... How many times to get the Nth element at the front of the resulting list? CAR...

Re: nth element of a lisp

This is e-lisp, not scheme or common lisp, I really don't expect homework. How about a little less evasion and snarkiness. In e-lisp, use the (nth ...) built-in function.

Re: nth element of a lisp

Like Cristopher Oliver said, u can use the nth function.

[Function]
nth n list

(nth n list) returns the nth element of list, where the car of the list is the ``zeroth'' element. The argument n must be a non-negative integer. If the length of the list is not greater than n, then the result is (), that is, nil. (This is consistent with the idea that the car and cdr of () are each ().) For example:

(nth 0 '(foo bar gack)) => foo
(nth 1 '(foo bar gack)) => bar
(nth 3 '(foo bar gack)) => ()

Re: nth element of a lisp

Which is the best way to delete some element, whose number is known
from a list ? Is it the following ?

(delete-if (lambda (x) t) some-list :start i :count 1)

thanks