如何用函数式语言编写简单的树算法?

时间:2021-06-30 22:01:53

Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first given a list of keyword, and must then answer if another given word was in the list.

假设我想实现一个合理有效的“关键字识别算法”,首先给出一个关键字列表,然后必须回答列表中是否有另一个给定的词。

In an imperative language, I would store the keywords in a tree (one node per character). Then, when receiving a word to test, I would scan my tree to test if the word is a keyword.

在命令式语言中,我将关键字存储在树中(每个字符一个节点)。然后,当收到要测试的单词时,我会扫描我的树以测试该单词是否是关键字。

I'd like to understand how such an algorithm would be coded in a functional language. How does one get the benefits of 'stateless' programming while keeping the efficiency of 'imperative' algorithms. Isn't it necessary to store the tree somewhere between the lookups if you don't want to rebuild it each time?

我想了解这种算法如何用函数式语言编写。如何在保持“命令式”算法效率的同时,获得“无状态”编程的好处。如果您不想每次都重建它,是否有必要将树存储在查找之间的某个位置?

2 个解决方案

#1


2  

I think what you mean is a character per node... sort of like a simple hash tree scheme for keyword lookup. Assuming this or even another kind of tree... imagine doing something like this (in pseudo-LISP):

我认为你的意思是每个节点的一个字符......有点像关键字查找的简单哈希树方案。假设这个甚至是另一种树...想象做这样的事情(在伪LISP中):

(defun buildtree (wordlist) ...code to build tree recursively returns the tree...)
(define lookup (tree word) ...code to look up word using tree, returns t or nil...)

(defun lookupmany (tree querylist)
   (if (eq querylist nil)
       nil
       (cons (lookup tree (car querylist)) (lookupmany tree (cdr querylist))
   )
)

(defun main (wordlist querylist) ; the main entry point
   (lookupmany (buildtree wordlist) querylist)
)

if this is what you mean, this is fairly straight-forward functional programming. Is it really stateless? That's a matter of debate. Some people would say some forms of functional programming store what we normally call "state" on the stack. Moreover, Common LISP even since the first edition of the Steele book has had iterative constructs, and LISP has had setq for a long, long time.

如果这是你的意思,这是一个相当简单的函数式编程。它真的是无国籍的吗?这是一个有争议的问题。有些人会说某些形式的函数式编程存储了我们通常称之为“状态”的堆栈。此外,Common SISP甚至自第一版Steele书以来都有迭代结构,LISP已经有很长一段时间的setq。

But in the theory of programming languages, what we mean by "stateless" is pretty much satisfied by the idea shown here.

但是在编程语言理论中,我们所说的“无国籍”对于这里所展示的想法非常满意。

I think the above is something like the arrangement you mean.

我认为以上就像你的意思。

#2


0  

I imagine you'd want something like a tree with a list of children, as described here.

我想你会想要一个像孩子一样的树的东西,如这里所描述的。

#1


2  

I think what you mean is a character per node... sort of like a simple hash tree scheme for keyword lookup. Assuming this or even another kind of tree... imagine doing something like this (in pseudo-LISP):

我认为你的意思是每个节点的一个字符......有点像关键字查找的简单哈希树方案。假设这个甚至是另一种树...想象做这样的事情(在伪LISP中):

(defun buildtree (wordlist) ...code to build tree recursively returns the tree...)
(define lookup (tree word) ...code to look up word using tree, returns t or nil...)

(defun lookupmany (tree querylist)
   (if (eq querylist nil)
       nil
       (cons (lookup tree (car querylist)) (lookupmany tree (cdr querylist))
   )
)

(defun main (wordlist querylist) ; the main entry point
   (lookupmany (buildtree wordlist) querylist)
)

if this is what you mean, this is fairly straight-forward functional programming. Is it really stateless? That's a matter of debate. Some people would say some forms of functional programming store what we normally call "state" on the stack. Moreover, Common LISP even since the first edition of the Steele book has had iterative constructs, and LISP has had setq for a long, long time.

如果这是你的意思,这是一个相当简单的函数式编程。它真的是无国籍的吗?这是一个有争议的问题。有些人会说某些形式的函数式编程存储了我们通常称之为“状态”的堆栈。此外,Common SISP甚至自第一版Steele书以来都有迭代结构,LISP已经有很长一段时间的setq。

But in the theory of programming languages, what we mean by "stateless" is pretty much satisfied by the idea shown here.

但是在编程语言理论中,我们所说的“无国籍”对于这里所展示的想法非常满意。

I think the above is something like the arrangement you mean.

我认为以上就像你的意思。

#2


0  

I imagine you'd want something like a tree with a list of children, as described here.

我想你会想要一个像孩子一样的树的东西,如这里所描述的。