{- * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. -} module TypeDecl where import Prelude hiding ( Left , Right ) -- ---------------------------------------- type Pos = (Int, Int) data Color = Red | Green | Blue deriving (Show) data Move = Left | Right | Up | Down deriving (Show) move :: Move -> Pos -> Pos move Left (x, y) = (x - 1, y ) move Right (x, y) = (x + 1, y ) move Up (x, y) = (x, y - 1) move Down (x, y) = (x, y + 1) moves :: [Move] -> Pos -> Pos moves ms p0 = foldl (\ p m -> move m p) p0 ms moves' :: [Move] -> Pos -> Pos moves' = flip (foldl (flip move)) moves'' :: [Move] -> Pos -> Pos moves'' ms = foldl (>>>) id fs where fs = map move ms (>>>) = flip (.) ms :: [Move] ms = [Down, Left, Down, Right, Up] ms0, ms0', ms0'' :: Pos ms0 = moves ms (0,0) ms0' = moves' ms (0,0) ms0'' = moves'' ms (0,0) -- ---------------------------------------- data Shape = Circle Float | Rect Float Float deriving (Show) square :: Float -> Shape square n = Rect n n area :: Shape -> Float area (Circle r) = pi * r * r area (Rect w h) = w * h -- ---------------------------------------- safeDiv :: Int -> Int -> Maybe Int safeDiv _ 0 = Nothing safeDiv x y = Just (x `div` y) -- ---------------------------------------- data Nat = Zero | Succ Nat deriving (Show) intToNat :: Int -> Nat intToNat 0 = Zero intToNat n = Succ (intToNat (n-1)) natToInt :: Nat -> Int natToInt Zero = 0 natToInt (Succ n) = 1 + natToInt n add :: Nat -> Nat -> Nat add Zero n = n add (Succ m) n = Succ (add m n) sub :: Nat -> Nat -> Nat sub n Zero = n sub (Succ m) (Succ n) = sub m n sub _ _ = error "negative numbers not allowed" mul :: Nat -> Nat -> Nat mul Zero _n = Zero mul (Succ m) n = (m `mul` n) `add` n -- ---------------------------------------- data List a = Nil | Cons a (List a) deriving (Show) -- ---------------------------------------- data IntTree = Leaf | Node IntTree Int IntTree deriving (Show) t :: IntTree t = Node (Node (Node Leaf 1 Leaf) 3 (Node Leaf 4 Leaf) ) 5 (Node (Node Leaf 6 Leaf) 7 (Node Leaf 9 Leaf) ) occurs :: Int -> IntTree -> Bool occurs _m Leaf = False occurs m (Node l n r) = m == n || occurs m l || occurs m r flatten :: IntTree -> [Int] flatten Leaf = [] flatten (Node l n r) = flatten l ++ [n] ++ flatten r -- ---------------------------------------- data Tree1 a = Leaf1 a | Node1 (Tree1 a) (Tree1 a) deriving (Show) data Tree2 a = Leaf2 | Node2 (Tree2 a) a (Tree2 a) deriving (Show) data Tree3 a b = Leaf3 a | Node3 (Tree3 a b) b (Tree3 a b) deriving (Show) data Tree4 a = Node4 a [Tree4 a] deriving (Show) -- ----------------------------------------