我试图检查列表是否在 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
,所以我有一个类型不匹配。我怎么能解决这个问题?
其次,我担心这种方法很幼稚,我该如何以更好的方式解决问题?
感谢您的时间,祝您有美好的一天!