gpt4 book ai didi

lisp - Mapcar 和 assoc

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

我想做的事:

(mapcar #'assoc '(a s) '((a . b) (c . d) (s . f)))

让它返回

((A . B) (S . F))

这似乎很合理,考虑到 (assoc 'a '((a . b) (c . d) (s . f))) 返回 (A . B)(assoc 's '((a . b) (c . d) (s . f))) 返回 (S . F)。但可惜它不起作用:

*** - ASSOC: A is not a list
The following restarts are available:
ABORT :R1 Abort main loop

有什么想法吗?

最佳答案

当与两个列表一起使用时,mapcar 将函数成对地应用到列表(对于三个列表,它应用它们三重等)。所以

(mapcar #'assoc '(a s) '((a . b) (c . d) (s . f)))

相同
( (assoc 'a (a . b)) (assoc 's (c . d)) )

(当与不同长度的列表一起使用时,mapcar 使用最小列表的大小)。为了得到你想要的,你应该这样做:

(mapcar (lambda (x) (assoc x '((a . b) (c . d) (s . f)))) '(a s))

关于lisp - Mapcar 和 assoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4840173/

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