sml - 以 ml 为单位的实数减法函数

标签 sml ml

我正在尝试在 moscow ml 中减去两个函数(均为实型)。然后它说“重载 - 不能应用于 real -> real 类型的参数。那么我应该如何编写函数?

fun CircleArea  x = x*x*Math.pi

fun SquareArea x:real = 4*x*x

fun Area x = SquareArea - CircleArea

最佳答案

您可能实际上并不想从一个函数中减去另一个函数,而是想在应用这些函数后返回这些函数的值。您可以通过以下方式实现此目的:

fun Area x = (SquareArea x) - (CircleArea x)

括号不是强制性的,因为函数应用程序(即 SquareAreax 之间的空间)比任何二元运算符(包括 -)绑定(bind)得更紧密>.

您应该考虑在 ML 中使用以下命名约定:常规函数的起始符号为小写,而代数类型的值构造函数的起始符号为大写。例如:

fun area x = squareArea x - circleArea x

但是:

datatype shape = Square of int * int
               | Circle of int

关于sml - 以 ml 为单位的实数减法函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18235034/

相关文章:

sml - 在函数表示的环境中查找值

functional-programming - Curry 函数出现问题 (SML/NJ)

sml - 你如何做模式匹配是标准 ML?

types - 返回 SML 中的自由(未绑定(bind))变量列表

ml - 将运算符传递给 ML 中的函数

data-structures - 在标准 ML O(n) 时间内追加到列表中吗?

haskell - 如何理解《纯函数式数据结构》中的分段二叉堆

sml - 机器学习抽象数据类型

sockets - 如何使用Socket.select

pattern-matching - SML 中的模式匹配?