针对Scheme中的列表列表运行查询

时间:2011-04-23 16:11:09

标签: list scheme matching

我陷入了项目的中间。我有一个列表,如:

'((a for apple) (b is book) (c in cat) (ronn live in NY))

现在我想以列表的形式进行查询,并在列表列表中显示正确的条目。例如,如果我输入'(a for what)'(what in cat),则会显示(a for apple)(c in cat)。如果我输入'(ronn live in where),则会显示(ronn live in NY)

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

如何在列表中运行filter例程,并使用使用查询信息初始化的lambda对象,然后将其应用于列表,直到找到匹配为止。

例如,你会有一个看起来像

的lamda
(define (filter-object query)
    (define (query-function list-input)
        ;;do something here for query function that will take the initialized
        ;;query and match it against the list-input to see if there's a match
        ;;it should return #t or #f
    )

    ;;the return of the filter-object is the query function
    query_function) 

;;my-filter-function is initialized with a specific query
(define my-filter-function (filter-object '(a for what)))

现在初始化filter-object,在列表中运行过滤器

(define (filter filter-function list-of-lists)
    (cond ((eq? list-of-lists '()) '())
          ((filter-function (car list-of-lists))
           (cons (car list-of-lists)
              (filter filter-function (cdr list-of-lists)))
          (else (filter filter-function (cdr list-of-lists))))

(filter my-filter-function my-list)

这应该返回匹配的单元素列表(假设您没有在列表集中放置多个副本。

希望这有帮助,

杰森

答案 1 :(得分:0)