输入:
'(("may 001" 75 72)
("may 002" 75 75)
("may 003" 70 73)
("june 101" 55 55)
("june 104" 55 54)
("aug 201" 220 220))
期望的输出:
'(("may 001" 75 72) ("may 002" 75 75) ("may 003" 70 73))
我如何实现这一目标?我只想要may
条款。
答案 0 :(得分:1)
根据其值获取特定元素的“正常”方法是使用filter
和合适的谓词。
这些方面的东西:
(define (may? str)
(string=? "may" (substring str 0 3)))
(define (only-may ls)
(filter (lambda (x) (may? (car x))) ls))
答案 1 :(得分:0)
为了有用,我们可能想要一个带有月份名称和记录列表的函数,并返回匹配记录的列表:
#lang racket
;; string ListOf(ListOf(String Int Int)) -> ListOf(ListOf(String Int Int))
(define (get-month target-month list-of-record)
;; ListOf(String Int Int) -> Boolean
(define (record-matches? record)
(regexp-match target-month (first record)))
(filter record-matches? list-of-record))
然后我们可以在查询中使用它,如:
"scratch.rkt"> (get-month "may"
'(("may 001" 75 72)
("may 002" 75 75)
("may 003" 70 73)
("june 101" 55 55)
("june 104" 55 54)
("aug 201" 220 220)))
'(("may 001" 75 72) ("may 002" 75 75) ("may 003" 70 73))
"scratch.rkt"> (get-month "april"
'(("may 001" 75 72)
("may 002" 75 75)
("may 003" 70 73)
("june 101" 55 55)
("june 104" 55 54)
("aug 201" 220 220)))
'()