David
David

Reputation: 11

OCaml check if a list is sorted

need help on checking weather list is sorted in ocaml

does this work?

let issorted x = match x with 
    [] -> true
    | _::[] -> true
    | _::_ -> issorted_helper (x)
;; 

let rec issorted_helper x = match x with
    | [] -> true
    | h::t ->
        if h > t
            false
        else
            issorted_helper(t)
;;

Upvotes: 0

Views: 972

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66803

It's an odd way to ask your question. Have you tried your code? By eye, it looks to me like it won't compile. Hint: you're using the > operator to compare values of two different types.

Update

Since you say you don't have access to OCaml yet, I'd say you should wait until you do. It's not so useful for us to act as your compiler! This code is very close, you'll get it working quickly once you have your OCaml.

Upvotes: 2

Related Questions