Re: Functional programming in Python

2001-05-20 Thread Ketil Malde

Manuel M. T. Chakravarty [EMAIL PROTECTED] writes:

 You want to be able to write

   f 1 2 + g 3 4

 instead of

   (f 1 2) + (g 3 4)

I do?  Personally, I find it a bit confusing, and I still often get it
wrong on the first attempt.  The good thing is that the rule is simple
to remember. :-)

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



RE: HUGS error: Unresolved overloading

2001-05-20 Thread Mark P Jones

Hi David,

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

I'm branching away from your question, but hope that you might find some
additional comments useful ... the last equation in your definition can
actually be expressed more succinctly as:

  isSorted (x1:x2:xs) = (x1 = 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 = 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).

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


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe