next up previous
Next: About this document ... Up: Accompanying material for: A Previous: Implementation

Basic types and functions

This module contains the type of bindings and the auxiliary function foldm (and some others) listed in the appendix of the paper.

>  module Basic(

>      Binding((:->)), key, prio,
>      o, foldm, inrange, guard, readWithDefault)
>  where

Bindings.

>  data Binding k p              =  k :-> p


>  key                           :: Binding k p -> k
>  key  (k :-> _p)               =  k

>  prio                          :: Binding k p -> p
>  prio (_k :-> p)               =  p

Function composition.

>  infix 0 :->


>  o                             :: (b -> c) -> (a -> b) -> (a -> c)
>  f `o` g                       =  a -> f (g a)

Folding a list in a binary-subdivision scheme.

>  foldm                         :: (a -> a -> a) -> a -> [a] -> a

>  foldm (*) e x
>    | null x                    =  e
>    | otherwise                 =  fst (rec (length x) x)
>    where rec 1 (a : as)        =  (a, as)
>          rec n as              =  (a1 * a2, as2)
>            where m             =  n `div` 2
>                  (a1, as1)     =  rec (n - m) as 
>                  (a2, as2)     =  rec m       as1


>  inrange                       :: (Ord a) => a -> (a, a) -> Bool

>  a `inrange` (l, r)            =  l <= a && a <= r


>  guard                         :: Bool -> [a] -> [a]

>  guard False _as               =  []
>  guard True  as                =  as

The variant readWithDefault returns a given default value if the read is not successful.

>  readWithDefault               :: (Read a) => a -> String -> a

>  readWithDefault def s         =  case reads s of
>      [(a, "")]                 -> a
>      _                         -> def


next up previous
Next: About this document ... Up: Accompanying material for: A Previous: Implementation
Ralf Hinze 2001-03-20