Page 1 of 1

nth element of a lisp

Posted: Mon Sep 07, 2009 3:15 am
by sugar
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

Posted: Tue Sep 08, 2009 2:43 pm
by nuntius
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

Posted: Tue Sep 08, 2009 5:14 pm
by omouse
You would use a recursive function to get the nth element of the list using only CAR and CDR.

Click to find out more about RECURSION.

Click to find out more about the CAR function.

Click to find out more about the CDR function.

Re: nth element of a lisp

Posted: Thu Sep 10, 2009 7:06 pm
by Christopher Oliver
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

Posted: Sat Oct 31, 2009 1:42 pm
by Johny22
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

Posted: Fri Nov 04, 2011 12:50 am
by Jacey
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