【问题标题】:racket/scheme filtering球拍/计划过滤
【发布时间】:2012-11-22 00:37:30
【问题描述】:

如何过滤这个以显示所有蔬菜?提前谢谢你。

("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")))

【问题讨论】:

    标签: filter scheme racket


    【解决方案1】:

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

    (filter (lambda (x) (string=? "vegetable" (caddr x)))
            lst)
    
    => '(("Chile" 1965 "vegetable" . "NM")
         ("WaterMelon" 2007 "vegetable" . "OK")
         ("Jalapeno" 1995 "vegetable" . "TX"))
    

    【讨论】:

    • 非常感谢!这正是我开始所需要的
    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2011-10-07
    • 2015-12-20
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多