1

Morning -

I'm trying to see if there is a way to check if an item returned from a list is a list itself.

For example

(elt '(a (b c) d) 1)

Is there a way to check that the item returned is a list?

I tried using length,

(length (elt '(a (b c) d) 1))

but if it is given an item that is not a list it throws an error.

In short I'm looking for a way to go through each element of a list and check if it is a list or not.

Thank you

Freddy
  • 2,249
  • 1
  • 22
  • 31

1 Answers1

9

the listp tells you that :

(listp (elt '(a (b c) d) 1))
meirrav
  • 761
  • 1
  • 9
  • 27
  • The sad thing is that I have been using integerp for a bit.. -1 myself for overlooking the obvious. – Freddy Sep 20 '12 at 12:15
  • 4
    Nice, I like that `listp` in CL in O(1), since dotted and circular lists are considered lists too. In the Scheme world, `list?` only returns true for proper lists, so (other than implementations with immutable conses, like Racket), `list?` is O(n), which means that usually I'm hesitant to suggest the use of `list?` for general use. – C. K. Young Sep 20 '12 at 13:04