问题描述
我想在特定条件下从列表列表中获得一些列表.例如:
I would like to have some lists from a list of lists under a certain condition. For example:
`((a w) (e w))`
来自
`((a w) (e w) (i u) (o u))`
条件是在第二个位置有 'w.
where the condition is to have 'w in the second position.
我的代码
(filter (andmap (equal? (cdr lst) 'w)) 'lst)
与
(define lst '((a w) (e w) (i u) (o u)))
不工作.我做错了什么?
is not working. What am I doing wrong?
推荐答案
您不应该引用列表:'lst
与 lst
不同.要访问第二个元素,请使用 second
(如果未定义,请改用 cadr
).使用cdr
,您可以获得列表的rest(这是另一个列表),而不是它的第二个元素.哦,filter
需要一个 lambda
作为它的第一个参数,而不是 andmap
.这就是我的意思:
You should not quote the list: 'lst
is not the same as lst
. And to access the second element, use second
(if that's not defined, use cadr
instead). With cdr
you get the rest of the list (which is another list), not its second element. Oh, and filter
expects a lambda
as its first argument, not andmap
. This is what I mean:
(define lst '((a w) (e w) (i u) (o u)))
(filter (lambda (sl) (equal? (second sl) 'w))
lst)
=> '((a w) (e w))
这篇关于从列表列表中选择一些列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!