'any' and 'all' compared with the rest of the Report

Sven Panne Sven.Panne@informatik.uni-muenchen.de
Tue, 23 Jan 2001 23:10:53 +0100


Iavor Diatchki wrote:
> [...] but in practise programs run slower.

If "practise" = "simple interpreter", yes. But...

> this is where i would expect a good compiler to do some optimisation,
> i.e to remove the need for the intermediate list.

<TotallyUnbiasedAd>
   Given

      or = foldr (||) False
      any p = or . map p

   ghc -O generates basically the following code (use -ddump-simpl to
see this):

      any :: (a -> Bool) -> [a] -> Bool
      any p xs = let go ys = case ys of
                                (z:zs) -> case p z of
                                             False -> go zs
                                             True  -> True
                 in go xs

   This is exactly the recursive version, without any intermediate list,
   but I hardly think that anybody recognizes this with a quick glance
only.
</ToTallyUnbiasedAd>
 
> i like the idea of programming at a higher level, as i believe it
> produces "better structured" programs.  what i mean is that one
> manages to capture certain aspects of a program, which would
> be obscured if one always used explicit recursion.  i think
> higher order functions like maps and folds really go a long way
> toward structuring functional programs.  in a way this is simillar
> to using loops instead of explicit gotos in procedural programs.
> anyways these are my deep thought on the matter :)

IMHO you should write any kind of recursion over a given data structure
at most once (in a higher order function). (Constructor) classes and
generics are a further step in this direction. It vastly improves
readability (after you get used to it :-) and often there is no
performance
hit at all.

Cheers,
   Sven