简体   繁体   English

Scheme中的递归搜索

[英]Recursive search in Scheme

I'm currently stuck with the following problem : 我目前遇到以下问题:

I have a database of n elements and I want to do a recursive search so that I will get all the possible matches . 我有n个元素的数据库,我想进行递归搜索,以便获得所有可能的匹配项。

So , lets say , I get k matches for the first pattern . 因此,可以说,我得到第一个模式的k个匹配项。 For every k matches I found I re-search the database with the next pattern and get the new association lists ....and so on . 对于每找到的k个匹配项,我都会使用下一个模式重新搜索数据库,并获取新的关联列表....依此类推。 This is my problem , I cannot make a function that will give me all the results . 这是我的问题,我无法做一个能给我所有结果的函数。

I really cannot get myself to think of a "plan" to attack this problem . 我真的无法使自己想到解决这个问题的“计划”。 I always wonder how to save my curent assoc-list and , at the same time , remove it when I get to the end. 我一直想知道如何保存当前的assoc-list,并在结束时将其同时删除。

Summary of my idea: If I have a database=db and need to match n patterns . 我的想法摘要:如果我有一个database = db并且需要匹配n个模式。 I start with pattern 0 , get k assoc-list and I want to move forward to match pattern 1 , having in mind I have k assoc-lists from the previous,. 我从模式0开始,获取k个assoc-list,我想向前移动以匹配模式1,请记住我有前一个的k个assoc-list。 I finish pattern 1 and get M assoc-list , for every m assoc-list I go forward ... In the end I either get a assoc-list of size n(number of patterns) or get false. 我完成模式1并得到M assoc-list,对于前进的每一个m assoc-list ...最后,我要么得到大小为n(模式数)的assoc-list,要么为false。

I really only want some ideas so I can get past this "brick wall". 我真的只想要一些想法,这样我才能克服这堵“砖墙”。 Please judge,stab,kill my idea , anything. 请判断,刺,杀死我的想法,任何事情。 Thank you. 谢谢。

I am not certain whether you wish to narrow or broaden your search based on subsequent searches. 我不确定您是否希望根据后续搜索来缩小或扩大搜索范围。 Here is pseudocode that broadens: 这是扩展的伪代码:

recursive_search (list_of_patterns)
  if is_empty(list_of_patterns)
    return empty_list
  pattern = pop_first(list_of_patterns)
  return query(pattern) + recursive_search(list_of_patterns)

EDIT to keep your pattern with the query as an alist... my scheme-foo is weak, but here goes: 编辑以使您的模式与查询保持一致...我的scheme-foo很弱,但这里有:

(define query_alist (lambda x)(x . query-exec(x)))

(define r_query (lambda pattern__list)
  (cond
    ((null pattern_list) ())
    (else ((query_alist (first pattern_list) (r_query (rest pattern_list)))
  )
) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM