
Laszlo Nemeth wrote:
isSorted xs = and (zipWith (<=) 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)."
That's right ... under cbn! At the same time David's version with explicit recursion is fine both in Hugs and in a strict language.
I recently started using Caml for day to day work and I get bitten because of the 'lazy mindset' at least once a week. I am not in disagreement with you over the style, but explicit recursion in this case avoids the problem.
I find this remark very interesting. It reminds me of the many people who say that they want a call-by-value language that allows call-by-need annotations, because you rarely need call-by-need. That depends very much on what you mean by `need call-by-need'! Mark has given a nice example how a function can be defined concisely, taking advantage of the call-by-need language. I very much disagree with Lazlo's comment, that you should use explicit recursion to make the definition valid for call-by-value languages as well. You should take full advantage of the expressive power of your call-by-need language. Otherwise, why not avoid higher-order functions because they are not available in other languages? As Lazlo says, you need a 'lazy mindset' to use this style. It takes time to develop this 'lazy mindset' (just as it takes time to lose it again ;-). To help people understanding and learning this 'lazy mindset', I'd really like to see more examples such as Mark's. If there are more examples, I could collect them on haskell.org.
PS. Why not go all the way
and . uncurry (zipWith (<=)) . id >< tail . dup
with appropriate definitions for dup and >< (prod)?
It is longer, uses more functions and doesn't make the algorithm any clearer. IMHO point-free programming and categorical combinators are not the way to obtain readable programs. Mark uses few standard functions. His definition is more readable than the recursive one, because it is shorter and because it turns implicit control into explicit data structures. Cheers, Olaf -- OLAF CHITIL, Dept. of Computer Science, University of York, York YO10 5DD, UK. URL: http://www.cs.york.ac.uk/~olaf/ Tel: +44 1904 434756; Fax: +44 1904 432767