简体   繁体   English

标准ML:二进制搜索树中的查找功能

[英]Standard ML: Lookup Function in Binary Search Tree

I'm making my way through Ullman's Elements of ML Programming. 我正在通过Ullman的ML编程元素。 He introduces a datatype for a BST in ch. 他在ch中介绍了BST的数据类型。 6 as follows: 6如下:

datatype 'label btree = 
    Empty |
    Node of 'label * 'label btree * 'label btree;

Then he defines a lookup function to tell whether a node with a given label exists within the BST: 然后他定义了一个查找函数来判断BST中是否存在具有给定标签的节点:

fun lookup lt Empty x = false
    | lookup lt (Node(y, left, right)) x =
        if lt(x, y) then lookup lt left x
        else if lt(y, x) then lookup lt right x
        else true;

ML tells us that the function is of type: ML告诉我们函数的类型是:

val lookup = fn : ('a * 'a -> bool) -> 'a btree -> 'a -> bool

1) I'm having trouble parsing what the above means. 1)我在解析上述内容时遇到问题。 I know "->" associates from the right, but I'm having trouble sorting out how to do this. 我知道“ - >”右边的同事,但是我很难理解如何做到这一点。 How would you know how to do this just by looking at the above? 你怎么知道怎么做才看到上面这个?

val lookup = fn : ('a * 'a -> bool) -> ('a btree) -> ('a) -> (bool)

2) I'm confused though, because I thought curried functions create a chain of functions that return another function with each subsequent argument. 2)我很困惑,因为我认为curried函数创建了一个函数链,每个后续参数返回另一个函数。 But based on the types ML is giving me above, it looks like it's not curried. 但根据ML给我的类型,看起来它不是咖喱。 Any idea what's going on here? 知道这里发生了什么吗?

Thanks for the help, bclayman 谢谢你的帮助,bclayman

The type val lookup = fn : ('a * 'a -> bool) -> 'a btree -> 'a -> bool tells us that it takes an argument of type ('a * 'a -> bool) and returns a function of type 'a btree -> 'a -> bool . 类型val lookup = fn : ('a * 'a -> bool) -> 'a btree -> 'a -> bool告诉我们它需要一个类型的参数('a * 'a -> bool)并返回类型'a btree -> 'a -> bool的函数。 This function then takes in something of type 'a tree and returns a function of type 'a -> bool , which then takes in an argument of type 'a and returns a bool . 然后,这个函数接受某种类型的'a tree并返回一个类型为'a -> bool的函数,然后它接收一个类型为'a的参数并返回一个bool So yes, the lookup function is curried. 所以是的, lookup函数是curry。 The first parameter (which is a function as well), however, is not curried as it takes in two arguments, wrapped up in a pair. 然而,第一个参数(也是一个函数)并不是curry,因为它包含两个参数,包含在一对中。 In general, you can take a curried function (ie one that takes an argument and returns a function) and transform it into an uncurried function in the following way: 通常,您可以采用curried函数(即接受参数并返回函数的函数)并按以下方式将其转换为未计算函数:

Let's say you have the following function 假设您有以下功能

fun f e1 e2 e3 ... = e

of type 类型

'a -> 'b -> 'c -> ... -> 'res

We can uncurry this function by wrapping all the inputs in a tuple, such as 我们可以通过将所有输入包装在元组中来解决此问题,例如

fun f(e1, e2, e3, ...) = e

Which will then have the type 然后将具有该类型

'a * 'b * 'c * ... -> 'res

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

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