[英]Binary Search Tree insert function in OCaml
Suppose we want to insert value smaller.假设我们想插入更小的值。 It will go to Node (insert x left, k, right)
I don't understand how we can have insert x left
when function insert is declared as taking only one argument, the key.它将转到Node (insert x left, k, right)
我不明白当函数 insert 被声明为只接受一个参数,即键时,我们怎么能insert x left
。 How can left also be passed to insert funtion?怎么也可以把left传给insert函数呢?
type 'a bst_t =
| Leaf
| Node of 'a bst_t * 'a * 'a bst_t
let rec insert x = function
| Leaf -> Node (Leaf, x, Leaf)
| Node (left, k, right) ->
if x < k then Node (insert x left, k, right)
else Node (left, k, insert x right)
Ocaml has a REPL, ie an interactive environment useful for experimenting and having a conversation with ocaml . Ocaml 有一个 REPL,即一个交互式环境,可用于试验和与 ocaml 进行对话。
$ ocaml
OCaml version 4.02.3
# type 'a bst_t =
| Leaf
| Node of 'a bst_t * 'a * 'a bst_t ;;
type 'a bst_t = Leaf | Node of 'a bst_t * 'a * 'a bst_t
# let rec insert x = function
| Leaf -> Node (Leaf, x, Leaf)
| Node (left, k, right) ->
if x < k then Node (insert x left, k, right)
else Node (left, k, insert x right) ;;
val insert : 'a -> 'a bst_t -> 'a bst_t = <fun>
The read-eval-print-loop shows not only the value of the evaluated expression but also its type. read-eval-print-loop 不仅显示评估表达式的值,还显示其类型。 Here, you can see that the symbol insert
is bound to a function which takes a value of "some type 'a
" and returns another function that takes a value of "a binary tree of that type 'a
" and returns a value of the same binary tree of 'a
type.在这里,你可以看到符号insert
被绑定到以“某种类型的值的功能'a
”,并返回另一个函数,它的值“类型的二叉树'a
”,并返回的值'a
类型的相同二叉树。
I strongly recommend to use REPLs whenever you have them, as they tell you a lot about the system.我强烈建议您在拥有 REPL 时使用它们,因为它们会告诉您很多有关系统的信息。
The function
keyword defines a function as a series of patterns, and is almost always used without giving the parameter a name as such. function
关键字将function
定义为一系列模式,并且几乎总是在不给参数名称的情况下使用。 So the insert
function actually has two parameters.所以insert
函数实际上有两个参数。
This definition without function
is equivalent:这个没有function
定义是等价的:
let rec insert x node =
match node with
| Leaf -> Node (Leaf, x, Leaf)
| Node (left, k, right) ->
if x < k then Node (insert x left, k, right)
else Node (left, k, insert x right)
Although it might seem so at first sight, function insert
has not one but two arguments - one is the key to be inserted as you said, and the second one is the bst_t in which you want to insert the key.虽然乍一看似乎如此,但函数insert
不是一个而是两个参数 - 一个是您所说的要插入的键,第二个是您要插入键的 bst_t 。
In OCaml, the concept of Point-free programming is highly recommended when it's possible to apply it, and that's precisely what happens with your insert function.在 OCaml 中,当可以应用无点编程的概念时,强烈建议使用它,而这正是插入函数发生的情况。 Your version of the function is just a short-hand notation for the following function (written in normal, verbose syntax):您的函数版本只是以下函数的简写符号(以普通、冗长的语法编写):
let rec insert x bst = match bst with
| Leaf -> Node (Leaf, x, Leaf)
| Node (left, k, right) ->
if x < k then Node (insert x left, k, right)
else Node (left, k, insert x right)
where bst is the tree on which the insertion is performed, ie the tree that the function matches the Leaf | Node
其中 bst 是执行插入的树,即函数匹配Leaf | Node
的树Leaf | Node
Leaf | Node
patterns against. Leaf | Node
模式反对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.