【发布时间】:2019-11-11 22:22:59
【问题描述】:
我试图检查一个列表是否在 PolyML 中排序。该列表不是内置类型,而是由我定义为:
datatype list = empty | cons of int*list;
我不知道如何检查递增和递减顺序,所以现在我将自己限制为递增顺序(欢迎任何关于更通用解决方案的提示!)。
所以我的做法如下:
local
fun sortedIncreasing (empty) = 1000
| sortedIncreasing (cons(v,l)) = if(v < sortedIncreasing(l)) then v else Int.minInt
in
fun isSortedInc (empty) = true
| isSortedInc (cons(v,l)) = if (sortedIncreasing(cons(v,l)) = Int.minInt) then false else true
end;
首先Int.minInt 不是Int 类型,所以我的类型不匹配。我该如何解决?
其次,我担心这种方法很幼稚,我该如何以更好的方式解决问题?
感谢您的宝贵时间,祝您有美好的一天!
【问题讨论】: