gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:47 27 4
gpt4 key购买 nike

嘿,我一直在寻找一段时间,但没有明确的方向。我知道它在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com