ocaml检查列表是否按排序顺序排列

时间:2014-03-22 00:23:47

标签: ocaml

我不确定如何处理此问题,但我需要检查列表是否按顺序排列。

这就是我在做的事情:

let rec is_sorted_aux l res = match l with
    [] -> res
  | (h1::h2::t) ->
    if (h2 > h1) then
      is_sorted_aux t false
    else 
      is_sorted_aux t true
;;

但这是比较1和& 2,3& 4等中的元素对,而不是所有元素与它们的邻居。

我该如何处理?

1 个答案:

答案 0 :(得分:1)

使用as

  | h1 :: (h2 :: _ as t) ->

此外,您可以在比较失败时立即返回,而不是携带false直到列表结尾。

你不应该忽视你得到的警告。

相关问题