Mark P Jones mpj@cse.ogi.edu
Sun, 20 May 2001 21:43:06 -0700

```Hi David,

| Can anyone shed some light on the following error? Thanks in advance.
|=20
| isSorted :: Ord a =3D> [a] -> Bool
| isSorted [] =3D True
| isSorted [x] =3D True
| isSorted (x1:x2:xs)
|         | x1 <=3D x2  =3D isSorted (x2:xs)
|         | otherwise =3D False

I'm branching away from your question, but hope that you might find some
actually be expressed more succinctly as:

isSorted (x1:x2:xs) =3D (x1 <=3D x2) && isSorted (x2:xs)

This means exactly the same thing, but, in my opinion at least, is much
clearer.  In fact there's a really nice way to redo the whole definition
in one line using standard prelude functions and no explicit recursion:

isSorted xs =3D and (zipWith (<=3D) xs (tail xs))

In other words: "When is a list xs sorted?  If each element in xs is
less than or equal to its successor in the list (i.e., the corresponding
element in tail xs)."

I know this definition may look obscure and overly terse if you're new
to either (a) the prelude functions used here or (b) the whole style of
programming.  But once you get used to it, the shorter definition will
actually seem much clearer than the original, focusing on the important
parts and avoiding unnecessary clutter.

I don't see many emails on this list about "programming style", so this
is something of an experiment.  If folks on the list find it interesting
and useful, perhaps we'll see more.  But if everybody else thinks this
kind of thing is a waste of space, then I guess this may be the last
such posting!

All the best,
Mark

```