import qualified Tree as T (tree1, fringe)
data Nat = Zero | Succ Nat
nat2int :: Nat -> Int
nat2int Zero = 0
nat2int (Succ n) = 1 + nat2int n
int2nat :: Int -> Nat
int2nat 0 = Zero
int2nat n = Succ (int2nat (n-1))
data Expr = Val Int | Add Expr Expr | Mul Expr Expr
size :: Expr -> Int
size (Val _) = 1
size (Add x y) = size x + size y
size (Mul x y) = size x + size y
instance Show Nat where
show n = "N" ++ show (nat2int n)
instance Show Expr where
show (Val n) = show n
show (Add e1 e2) = "(+ " ++ show e1 ++ " "
++ show e2 ++ ")"
show (Mul e1 e2) = "(* " ++ show e1 ++ " " ++ show e2 ++ ")"
data MMaybe a = MNothing | MJust a deriving Show
instance (Eq a) => Eq (MMaybe a) where
MNothing == MNothing = True
(MJust x) == (MJust y) = x==y
_ == _ = False
class Mappable f where
mmap :: (a->b) -> f a -> f b
instance Mappable [] where
mmap = map
instance Mappable MMaybe where
mmap f (MJust x) = MJust (f x)
mmap f MNothing = MNothing
{-
instance Mappable Expr where
mmap f (Val x) = Val (f x)
mmap f (Add x y) = Add (mmap x) (mmap y)
mmap f (Mul x y) = Mul (mmap x) (mmap y)
-}
map' :: (a->b) -> [a] -> [b]
map' f xs = [f x | x <- xs]
filter' :: (a -> Bool) -> [a] -> [a]
filter' p xs = [ x | x <- xs, p x]
foldr' :: (a -> b -> b) -> b -> [a] -> b
foldr' _ a [] = a
foldr' f a (x:xs) = f x (foldr' f a xs)
l2 :: (Floating a) => [a] -> a
l2 = \l -> sqrt (foldr (+) 0 (map (\x -> x*x) l))
lowerCase :: Char -> Bool
lowerCase = (`elem` ['a'..'z'])
(+/+) :: Int -> Int -> Int
x +/+ y = 2*x+y
infix 3 +/+
infixr 5 :+
data MList a = Empty | a :+ (MList a) deriving Show