lisp apply append

CSa*_*awy 3 lisp append apply

我转发此功能以从列表x中删除数字

(defun rm-nums (x)
  (cond
    ((null x) nil)
    (t (mapcar 'numberp x))))
Run Code Online (Sandbox Code Playgroud)

但是,当我输入(rm-nums '(32 A T 4 3 E)) 回报(T NIL NIL T T NIL)

我想要它而不是返回T或Nil,我希望它返回仅导致NIL的值[这不是数字]所以这个例子应该返回(A T E) 我应该使用mapcar WITHOUT recursion或iteration或bultin函数"remove-if "

我认为它与名为apply-append的东西有关但我对此一无所知.任何帮助?

Chr*_*ung 5

我认为你的课程考虑到了一点:

(defun my-remove-if (pred lst)
  (apply #'append (mapcar (lambda (x)
                            (and (not (funcall pred x))
                                 (list x)))
                          lst)))
Run Code Online (Sandbox Code Playgroud)

它采用applyappendmapcar,像你说的.用法示例:

(my-remove-if #'numberp '(32 a t 4 3 e))
=> (a t e)
Run Code Online (Sandbox Code Playgroud)

Rörd建议的更为惯用的解决方案:

(defun my-remove-if (pred lst)
  (mapcan (lambda (x)
            (and (not (funcall pred x))
                 (list x)))
          lst))
Run Code Online (Sandbox Code Playgroud)