import qualified Tree as TT hiding (fringe)
data MyMaybe a = MyNothing | MyJust a deriving Show
data Expr = Val Int
| Add Expr Expr
| Mul Expr Expr
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))
add :: Nat -> Nat -> Nat
add Zero n = n
add (Succ m) n = Succ (add m n)
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 ++ ")"
instance Eq a => Eq (MyMaybe a) where
MyNothing == MyNothing = True
(MyJust x) == (MyJust y) = x == y
_ == _ = False
class Mappable f where
mmap :: (a -> b) -> f a -> f b
instance Mappable MyMaybe where
mmap f (MyJust x) = MyJust (f x)
mmap _ MyNothing = MyNothing
instance Mappable [] where
mmap = map
foldr' :: (a -> b -> b) -> b -> [a] -> b
foldr' _ e [] = e
foldr' f e (x:xs) = f x (foldr' f e xs)
l2 :: (Floating a) => [a] -> a
l2 l = sqrt (foldr (+) 0 (map (\x -> x^2) l))
infixr :+
data MList a = Empty | a :+ MList a deriving Show
dropW :: (a -> Bool) -> [a] -> [a]
dropW _ [] = []
dropW p s@(x:xs) | p x = s
| otherwise = dropW p xs
bot = bot
g [1,2] = 1
g _ = 2
take' 0 _ = []
take' _ [] = []
take' n (x:xs) = x : take (n-1) xs
take1 _ [] = []
take1 0 _ = []
take1 n (x:xs) = x : take (n-1) xs