Lambda 返回通过将函数参数应用于与值/谓词参数相等的所有元素而创建的列表

Lambda 返回通过将函数参数应用于与值/谓词参数相等的所有元素而创建的列表,lambda,lisp,common-lisp,Lambda,Lisp,Common Lisp,嘿,我已经找了一段时间了,没有明确的方向。我知道它在funcalls地区,但我遇到了麻烦。下面是我的两个问题。另外,我不确定是否应该将它们分成两个不同的线程。请帮忙。谢谢大家! ;; ;; returns a list created by applying its function parameter to all elements equal to the value parameter. All other elements remain unchanged ;; (defun fi

嘿,我已经找了一段时间了,没有明确的方向。我知道它在funcalls地区,但我遇到了麻烦。下面是我的两个问题。另外,我不确定是否应该将它们分成两个不同的线程。请帮忙。谢谢大家!

;;
;;  returns a list created by applying its function parameter to all elements equal to the value parameter. All other elements remain unchanged 
;;

(defun find-and-do (lst val fun)
  "(find-and-do '() 1 (lambda (x) (+ x 1))) → NIL
(find-and-do '(1 2 3) 2 (lambda (x) (* x 2))) → (1 4 3)
(find-and-do '(1 2 3) 2 (lambda (x) (* x 2))) → (1 4 3)
(find-and-do '(1 2 3 4) 2 #'sqrt) → (1 1.4142135623730951 3 4) 
(find-and-do '(a b c) 'b #'list) → (A (B) C) "

;(lambda (x) (funcall fun val ))) ; what I have so far
; I think id instead off val in the call above it would have to simultaneously pull the elements and modify them from a newly copied list
)

;;
;;  same as find-and-do, but instead of matching a value, apply the function parameter to those elements for which the predicate parameter applied results in true. 
;;

(defun test-and-do (lst predp fun)
  "(test-and-do '() #'evenp (lambda (x) (+ x 1))) → NIL
(test-and-do '(1 2 3 4) #'evenp (lambda (x) (* x 2))) → (1 4 3 8)"

; no idea
)

下面是我如何编写
测试和执行

(defun test-and-do (lst pred fun)
  (mapcar (lambda (x)
            (if (funcall pred x)
                (funcall fun x)
                x))
          lst))
(defun find-and-do (lst val fun)
  (test-and-do lst (lambda (x) (equal val x)) fun))
查找和执行
可以通过
测试和执行
来实现:

(defun test-and-do (lst pred fun)
  (mapcar (lambda (x)
            (if (funcall pred x)
                (funcall fun x)
                x))
          lst))
(defun find-and-do (lst val fun)
  (test-and-do lst (lambda (x) (equal val x)) fun))

非常感谢你!语法非常合理。将x等同于另一个前身的伟大想法。这个主题是什么?我在funcall lamba mapcar函数参数下查找,但只能直接使用它,而不能通过函数传递它。再次感谢!它看起来像主题“高阶函数”。谓词,而不是前置词。:-)