Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

racket/scheme filtering

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")))
like image 443
CSE Avatar asked Jun 21 '26 07:06

CSE


1 Answers

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"))
like image 134
Óscar López Avatar answered Jun 22 '26 23:06

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!