ML语法函数程序
可能的函数foo,其类型为
What could be the possible function foo, which has a type
’a * ’a -> int
即具有以下类型输出的函数
in ML. i.e. a function which has the following type of the output
这似乎是作业,所以我给你一个部分解决方案,一些提示。你想要的类型是'a *'a - > int
,所以一个合适的函数的骨架可能是这样的(我假设你使用标准ML):
This seems to be homework, so I give you a partial solution and some hints only. The type you want is a 'a * 'a -> int
, so the skeleton of a suitable function could be something like this (I assume you are using Standard ML):
fun foo(x, y) = ???
???
需求:它必须包含一个强制 x
和 y
具有相同类型的表达式,并且必须返回一个整数。后者不应该是硬的。对于前者,在SML中有很多可能性,例如,将它们放在同一个列表中,或者如果或从相同的
case
或句柄
。
The ???
needs to meet two requirements: it must contain an expression that forces x
and y
to have the same type, and it must return an integer. The latter shouldn't be hard. For the former, there are many possibilities in SML, e.g., putting them in the same list, or returning them from the branches of the same if
or case
or handle
.