2014-05-19 2 views
-2

숫자로 시작하는 모든 하위 목록을 가져오고 싶습니다. 그래서 그것은 nil 대신 ((3 f g h) (2 m n) (9 d)) 반환제 기능에 이상이 있는지 알고 싶습니다.

(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)) 

했다.

도움 주셔서 감사합니다.

답변

0

나는 이것이 당신이하려고했던 대략 무엇을 추측 :

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

을 다른 방법으로, remove-if-not을 사용할 수 있습니다 : 두 경우 모두

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

를, 그것은 예상대로 작동합니다

? (f '((3 f g h) l s (v k) (2 m n) (9 d) c)) 
((3 F G H) (2 M N) (9 D))