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

标签 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 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
)

最佳答案

下面是我编写test-and-do的方式:

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

find-and-do 可以根据test-and-do 来实现:

(defun find-and-do (lst val fun)
  (test-and-do lst (lambda (x) (equal val x)) fun))

关于lambda - 返回通过将函数参数应用于等于值/谓词参数的所有元素创建的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466706/

相关文章:

post - 无法在 LISP hunchentoot 中获取帖子

c# - 从列表中查找存在于另一个列表中的项目

java - 如何使用 lambda 表达式重构工厂类?

lambda - Java流: transform user input to batches

web-services - 使用 Lisp 的 Web 服务

package - 如何理解 Common Lisp 包的行为?

c# - 在动态 Lambda 表达式中获取 Count() 属性

lisp - ltk 的问题(普通 lisp)

recursion - Clojure,对向量列表求和,沿途记录位置

macros - Lisp 评估宏表达式中的变量