球拍/方案过滤 [英] racket/scheme filtering

查看:118
本文介绍了球拍/方案过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么过滤这个显示所有的蔬菜?

pre $ code>(Pecan。(1982)
$ b(Blackberry)(2004。(fruit。AL)))

(Peach。 ))bb b
(Rice)(2007。(grain。AR)))

(Orange。 )))

(Huckleberry。(2000。(fruit。ID)))

(Blackberry。(2004。 (水果,水果,LA)))

(WildBlueberry)。 (水果。我)))

(BlueCrab。(2000)(food。MD))

HoneycrispApple(2006。(fruit。MN)))

(Pumpkin。 b $ b(智利(1965)(蔬菜,NM)))

(蓝莓 (水果,ND)))

(WaterMelon。(2007。(vegetable。OK ))

(Pear。(2000。(fruit。or)))

(榛子 ))))

(Peach。(1984。(fruit。SC)))

(番茄。 (水果。TN)))

(Jalapeno。(1995。(vegetable.TX)))

Apple(2000。(fruit。WA)))


解决方案

将所有元素打包在一个列表中(在下面的代码中称为 lst ),并运行这个过滤器:


$ b $

 (filter(lambda(x)(string =?vegetable(caddr x)))
lst)

=> (智利1965蔬菜NM)
(WaterMelon2007蔬菜。OK)
(Jalapeno1995vegetable。TX


How would I filter this to display all vegetables? Thank you in advance.

("Pecan" . (1982 .("nut". "AL")))

("Blackberry" . (2004 .("fruit". "AL")))

("Peach" . (2006 .("fruit". "AL")))

("Rice" . (2007 .("grain". "AR")))

("Orange" . (2005 .("fruit". "FL")))

("Huckleberry" . (2000 .("fruit". "ID")))

("Blackberry" . (2004 .("fruit". "KY")))

("Strawberry" . (1980 .("fruit". "LA")))

("WildBlueberry" . (1991 .("fruit". "ME")))

("BlueCrab" . (2000 .("food". "MD")))

("HoneycrispApple" . (2006 .("fruit". "MN")))

("Pumpkin" . (2000 .("fruit". "NH")))

("Chile" . (1965 .("vegetable". "NM")))

("Blueberry" . (2001 .("fruit". "NC")))

("ChokeCherry" . (2007 .("fruit". "ND")))

("WaterMelon" . (2007 .("vegetable". "OK")))

("Pear" . (2000 .("fruit". "OR")))

("Hazelnut" . (2000 .("nut". "OR")))

("Peach" . (1984 .("fruit". "SC")))

("Tomato" . (2003 .("fruit". "TN")))

("Jalapeno" . (1995 .("vegetable". "TX")))

("Apple" . (2000 .("fruit". "WA")))

解决方案

Pack all of the elements in a single list (called lst in the code below) and run this filter on it:

(filter (lambda (x) (string=? "vegetable" (caddr x)))
        lst)

=> '(("Chile" 1965 "vegetable" . "NM")
     ("WaterMelon" 2007 "vegetable" . "OK")
     ("Jalapeno" 1995 "vegetable" . "TX"))

这篇关于球拍/方案过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆