packages feed

baskell-0.1: Prelude.bs

# The Baskell Prelude. Modelled off the Haskell Prelude.
#
# Author: Bernie Pope
# Date:   12 October 2004

all = \p -> compose andList (map p);

and = !x -> !y -> ite x (ite y True False) False;

andList = foldr and True;

any = \p -> compose orList (map p);

append 
   = !list1 -> \list2 -> 
        ite (null list1) 
            list2 
            (Cons (head list1) (append (tail list1) list2));

apply = !f -> \x -> f x;

compose = !f -> \g -> \x -> f (g x);

concat = foldr append [];

concatMap = \f -> compose concat (map f);

const = !x -> \y -> x;

curry = !f -> \x -> \y -> f (x,y);

cycle = !list -> append list (cycle list);

drop 
   = !num -> \list -> 
        ite (lte num 0) 
            list 
            (ite (null list) 
                 [] 
                 (drop (sub num 1) (tail list)));

elemI = compose any eqI;

filter 
   = \p -> !list -> 
        ite (null list) 
            [] 
            (ite (p (head list)) 
                 (Cons (head list) (filter p (tail list))) 
                 (filter p (tail list)));

flip = !f -> \x -> \y -> f y x;

foldl 
   = \f -> \acc -> !list -> 
        ite (null list) 
            acc 
            (foldl f (f acc (head list)) (tail list));

foldr 
   = \f -> \base -> !list -> 
        ite (null list) 
            base 
            (f (head list) (foldr f base (tail list)));

gte = !x -> !y -> or (gt x y) (eqI x y);

id :: a -> a;
id = \x -> x;

index 
   = !list -> !num -> 
        ite (eqI 0 num) 
            (head list) 
            (index (tail list) (sub num 1));

init 
   = !list -> 
        ite (singleton list) 
            [] 
            (Cons (head list) (init (tail list)));

iterate = \f -> \x -> Cons x (iterate f (f x));

last 
   = !list -> 
        ite (singleton list) 
            (head list) 
            (last (tail list));

length 
   = !list -> ite 
        (null list) 
        0 
        (plus 1 (length (tail list)));

lte = !x -> !y -> or (lt x y) (eqI x y);

map 
   = \f -> !list -> 
        ite (null list) 
            [] 
            (Cons (f (head list)) (map f (tail list)));

negate = !x -> sub 0 x;

not = !x -> ite x False True;

notElemI = compose all notEqI;

notEqI = !x -> !y -> not (eqI x y);

or = !x -> \y -> ite x True (ite y True False);

orList = foldr or False;

product = foldr mult 1;

repeat = \x -> Cons x (repeat x);

replicate = !num -> \x -> take num (repeat x);

reverse = foldl (flip Cons) [];

singleton = compose null tail;

sum = foldr plus 0;

take 
   = !num -> \list -> 
        ite (lte num 0) 
            [] 
            (ite (null list) 
                 [] 
                 (Cons (head list) (take (sub num 1) (tail list))));

uncurry = !f -> \pair -> f (fst pair) (snd pair);

undefined = undefined;

until = !p -> \f -> \x -> ite (p x) x (until p f (f x));

zip = zipWith (\a -> \b -> (a,b));

zipWith 
   = \f -> !list1 -> \list2 -> 
        ite (null list1) 
            [] 
            (ite (null list2) 
                 [] 
                 (Cons (f (head list1) (head list2)) 
                       (zipWith f (tail list1) (tail list2))));