How to verify if a list is sorted?
clojure, list, sorting
Solution
The simplest solution:
`(apply <= mylist)`
`>=` also works for reverse sorting
Problem
How can I, in Clojure, verify is a list of numbers is sorted? ``` (def my-list (list 1 2 3 1 4 2 2 4)) ``` `sorted?` only returns true if the collection implements the `sorted` interface. I was looking for a `reduce` operation that would iterate the list pairwise, such as `(reduce < my-list)`. I understand I could manually create pairs and compare these: ``` (letfn [(pair [l] (if (= (count l) 2) (list l) (cons (take 2 l) (pair (rest l)))))] (every? #(apply < %) (pair my-list))) ``` But that seems unnecessarily complex. It really seems to me as if I'm missing a basic function.