Source of mondrian.prelude

This is a pretty random collection of functions, the original work on Mondrian was done using “extreme programming”.

package mondrian.prelude;

/* OPERATIONS ON BOOLEANS */

not : Boolean -> Boolean;
not = b -> if (b) false else true;

xor : Boolean -> Boolean -> Boolean;
xor = a -> b -> if (a) (not b) else b;

/* Operations on Pairs */

class Pair
{  a : Object;
   b : Object;
};

fst : Pair -> Object;
fst = p ->
   switch(p)
   {  case Pair{x = a; y = b;} : x;
   };

snd : Pair -> Object;
snd = p ->
   switch(p)
   {  case Pair{x = a; y = b;} : y;
   };

/* OPERATIONS ON LISTS */

hd : List Object -> Object;
hd = l ->
   switch(l)
   {  case (h::t): h;
   };
   
tl : List Object -> List Object;
tl = l ->
   switch(l)
   {  case []: [];
      case (h::t): t;
   };
   
isNil : List Object -> Boolean;
isNil = l -> l == [];
   
concat : List Object -> List Object;
concat = foldr (++) [];

replicate : Integer -> Object -> List Object;
replicate = n -> x -> take n (repeat x);

repeat : Object -> List Object;
repeat = x -> let { xs = x :: xs; } in xs;

length : List -> Integer;
length = foldlStrict (n -> x -> n + 1) 0;

foldlStrict : (Object -> Object -> Object) -> Object -> List Object -> Object;
foldlStrict = f -> e -> xs ->
   switch (xs)
   {  case []: e;
      case (y::ys): let { g = f e y; } in g `seq` foldlStrict f g ys;
   };

(++) : List Object -> List Object -> List Object;
(++) = xs -> ys ->
   switch (xs)
   {  case []: ys;
      case (z::zs): z :: (zs ++ ys);
   };

reverse : List Object -> List Object;
reverse = inp -> rev1 inp [];

rev1 : List Object -> List Object -> List Object;
rev1 = inp -> acc ->
   switch(inp)
   {  case []:   acc;
      case (h::t):   rev1 t (h::acc);
   };
   
from : Integer -> List Integer;
from = n -> n `seq` (n :: from (n+1));

filter : (Object -> Bool) -> List Object -> List Object;
filter = p -> xs ->
   switch (xs)
   {  case []: [];
      case (y::ys):
           if (p y)
              y :: filter p ys
           else
              filter p ys;
   };

map : (Object -> Object) -> List Object -> List Object;
map = f -> xs ->
   switch (xs)
   {  case []: [];
      case (y::ys): f y :: map f ys;
   };

take : Integer -> List Object -> List Object;
take = n -> xs ->
   if (n == 0)
      []
   else
      switch (xs)
      {  case []: [];
         case (y::ys): y :: take (n-1) ys;
      };

drop : Integer -> List Object -> List Object;
drop = n -> xs ->
   if (n == 0)
      xs
   else
      switch (xs)
      {  case []: [];
         case (y::ys): drop (n-1) ys;
      };

foldr : (Object -> Object -> Object) -> Object -> List Object -> Object;
foldr = op -> e -> xs ->
   switch (xs)
   {  case []: e;
      case (y::ys): op y (foldr op e ys);
   };

showList : List Object -> IO;
showList = xs ->
   switch (xs)
   {  case []:
         {  putStr "[]";
         };

      default:
         {  putStr "[";
            showElements xs;
            putStr "]";
         };

   };

showElements : List Object -> IO;
showElements = xs ->
   switch (xs)
   {  case []:
         result (new Unit);
      case (y::ys):
         switch (ys)
         {  case []: putStr (show y);
            default:
               {  putStr (show y);
                  putStr ",";
                  showElements ys;
               };
         };
   };

/* OTHER OPERATIONS */

(#) = obj -> method -> method obj;

Last modified: 15/10/2001