针对方案中的列表列表运行查询

发布于 2024-11-02 21:28:30 字数 378 浏览 0 评论 0原文

我被困在我的项目中。我有一个列表列表,例如:

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

现在我想以列表的形式进行查询,并让它在我的列表列表中显示正确的条目。例如,如果我输入 '(a for what)'(what in cat) 它将显示 (a for apple) 或 <代码>(猫中的c)。如果我输入'(ronn live in where),它将显示(ronn live in NY)

谁能帮我解决这个问题吗?

I'm stuck in the middle of my project. I have a list of lists like:

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

Now I want to make a query in the form of a list and have it display the correct entry in my list of lists. For example, if I input '(a for what) or '(what in cat) it will display (a for apple) or (c in cat). If I input '(ronn live in where) it will show (ronn live in NY).

Can anyone help me solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-11-09 21:28:30

如何在列表中运行 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)

这应该返回一个匹配的单元素列表(假设您是不要在您的列表集中放置多个副本,

希望这会有所帮助,

杰森。

How about running a filter routine across the list, and using a lambda object initialized with your query information that will then be applied to the list until it finds a match.

So for instance, you would have a lamda that would look something like

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

Now with the filter-object initialized, run a filter across your list

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

This should return a one-element list of the matches (providing you aren't placing more than one copy in your list set.

Hope this helps,

Jason

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文