A Scheme style solution using recursion would look like:
Code: Select all
(defun sublistp (arg)
(and (consp arg)
(or (listp (car arg))
(sublistp (cdr arg)))))
If the first element (car arg) is not a list, then SUBLISTP will be called recursively with the rest of the list (cdr arg) as argument. The recursive call will then test the second element of the original list, which is the first element in the recursive call. This will go on until either an element is a list or the last element is reached and SUBLISTP will be called recursively with an argument of (cdr arg) => NIL, which will fail the (consp arg) test.
But again there is the problem that NIL elements are recognized as sublists because NIL also represents the empty list:
Code: Select all
(sublistp '(1 2 3)) => NIL
(sublistp '(1 (2 3))) => T
(sublistp '(1 2 nil)) => T ; is that correct?
Whether the last result is correct or not depends on the particular use-case, there is no clear "yes" or "no" answer.
Testing for non-empty sublists would look like:
Code: Select all
(defun sublistp (arg)
(and (consp arg)
(or (consp (car arg))
(sublistp (cdr arg)))))
This version will produce:
Code: Select all
(sublistp '(1 2 3)) => NIL
(sublistp '(1 (2 3))) => T
(sublistp '(1 2 nil)) => NIL ; different than above
- edgar