简体   繁体   English

了解sml语言的语法

[英]understand syntax in the sml language

Hello I started to write in sml and I have some difficulty in understanding a particular function. 您好,我开始用sml编写,但在理解特定功能方面有些困难。

I have this function: 我有这个功能:

fun isInRow (r:int) ((x,y)) = x=r;

I would be happy to get explain to some points: 我很乐意解释一些问题:

  1. What the function accepts and what it returns. 函数接受什么,返回什么。

  2. What is the relationship between (r: int) ((x, y)) . (r: int) ((x, y))之间是什么关系。

Thanks very much !!! 非常感谢 !!!

The function isInRow has two arguments. 函数isInRow有两个参数。 The first is named r . 第一个名为r The second is a pair (x, y) . 第二个是对(x, y) The type ascription (r: int) says that r must be an int. 类型说明(r: int)表示r必须是int。

This function is curried, which is a little unusual for SML. 该函数是可固化的,这对于SML来说有点不寻常。 What this means roughly speaking is that it accepts arguments given separately rather than supplied as a pair. 粗略地讲,这意味着它接受单独给定而不是成对提供的参数。

So, the function accepts an int and a pair whose first element is an int. 因此,该函数接受一个int和一对第一个元素为int的对。 These are accepted as separate arguments. 这些被接受为单独的参数。 It returns a boolean value (the result of the comparison x = r ). 它返回一个布尔值(比较的结果x = r )。

A call to the function would look like this: 对该函数的调用如下所示:

isInRow 3 (3, 4)

There is more to say about currying (which is kind of cool), but I hope this is enough to get you going. 关于curry还有更多的话要说(这很酷),但是我希望这足以使您前进。

In addition to what Jeffrey has said, 除了杰弗里所说的,

  • You don't need the extra set of parentheses: 您不需要额外的括号:

     fun isInRow (r:int) (x,y) = x=r; 
  • You don't need to specify the type :int . 您不需要指定类型:int If you instead write: 如果您改为:

     fun isInRow r (x,y) = x=r; 

    then the function's changes type from int → (int • 'a) → bool into ''a → (''a • 'b) → bool , meaning that r and x can have any type that can be compared for equality (not just int), and y can still be anything since it is still disregarded. 然后函数将类型从int→(int•'a)→bool更改为''a→(''a•'b)→bool ,这意味着rx可以具有可以比较相等性的任何类型(而不仅仅是int),并且y仍然可以是任何东西,因为它仍然被忽略。

    Polymorphic functions are one of the strengths of typed, functional languages like SML. 多态函数是类型化的功能语言(例如SML)的优势之一。

  • You could even refrain from giving y a name: 您甚至可以避免给y命名:

     fun isInRow r (x,_) = x=r; 

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

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