球拍:在结构列表中的结构字段上应用过滤器 [英] Racket: applying filter on a field of a struct in a list of struct
问题描述
请在下面找到我的代码片段:
Please find my code snippet below:
(define (try los)
(filter (string=? (person-name (first los)) "Mike") los))
我在这里遇到了一些语法错误.我不确定如何在结构列表中的结构的 1 个特定字段上应用过滤器.(注意:我不想使用递归).有人可以给我一个相同的例子或帮助我纠正我在这里尝试做的事情吗?
I am getting some syntactical errors here. I am not sure how to apply filter on 1 particular field of struct in a list of structs. (Note: I do not want to use recursion). Can someone give me an example for the same or help me correct what I am trying to do here?
推荐答案
需要在filter中定义lambda函数:
You need to define lambda function in filter:
(filter
(lambda(x) (string=? (person-name x)
"Mike"))
los)
这样的 lambda 函数非常有用,经常与 'map'、'ormap'、'andmap'、'filter'、'for-each'、'apply'、'memf'、'findf'、'foldl' 一起使用, 'foldr' 等对给定列表的每个成员进行操作.请参阅此处了解许多示例(搜索 lambda).
Such lambda functions are very useful and often used with 'map', 'ormap', 'andmap', 'filter', 'for-each', 'apply', 'memf', 'findf', 'foldl', 'foldr' etc to act on each member of the given list. See here for many examples (search for lambda).
这篇关于球拍:在结构列表中的结构字段上应用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!