-2

I want to get all sublists that start with a number. So I did

(defun function (list)
  (cond
    ((atom list) nil)
    ((and (numberq (car list)) (consp (car list))) 
      (cons (function (car list)) (number (cdr list))) ) 
    ((not (and (numberq (car list)) (consp (car list)))) (function (cdr list))) ) )

(function '((3 f g h) l s (v k) (2 m n) (9 d) c))

It returns nil instead of ((3 f g h) (2 m n) (9 d)).

Thank you for your help!

uselpa
  • 18,732
  • 2
  • 34
  • 52

1 Answers1

0

I guess this is roughly what you were trying to do:

(defun f (lst)
  (when lst
    (let ((e (car lst)))
      (if (and (consp e) (numberp (car e)))
        (cons e (f (cdr lst)))
        (f (cdr lst))))))

Alternatively, you can use remove-if-not:

(defun f (lst)
  (remove-if-not 
   (lambda (e) (and (consp e) (numberp (car e)))) 
   lst))

In both cases, it works as expected:

? (f '((3 f g h) l s (v k) (2 m n) (9 d) c))
((3 F G H) (2 M N) (9 D))
uselpa
  • 18,732
  • 2
  • 34
  • 52