All pastes #201035 Raw Edit

Miscellany

public text v1 · immutable
#201035 ·published 2006-10-13 15:30 UTC
rendered paste body
-- Practicum 5
-- Joris Bontje

-- Opgave 12.8 blz 219 (236)
----------------------------

-- commented so it won't conflict with existing code

--instance (Ord a, Ord b) => Ord (a,b) where
--   compare (a1, b1) (a2, b2) 
--    | compare a1 a2 == EQ = compare b1 b2
--    | otherwise = compare a1 a2

--instance Ord b => Ord [b] where
--   compare [] (_:_)  = LT
--   compare [] []     = EQ
--   compare (_:_) []     = GT
--   compare (x:xs) (y:ys)
--    | compare x y == EQ = compare xs ys
--    | otherwise = compare x y

-- Opgave 14.16
----------------------------

-- commented so it doesn't conflict with 14.17

--data Expr = Lit Int |
--            Add Expr Expr |
--            Sub Expr Expr

--size :: Expr -> Int
--size (Lit n) = 0
--size (Add e1 e2) = 1 + size e1 + size e2
--size (Sub e1 e2) = 1 + size e1 + size e2

-- Opgave 14.17
----------------------------

data Expr = Lit Int |
            Add Expr Expr |
            Sub Expr Expr |
            Mul Expr Expr |
            Div Expr Expr

eval :: Expr -> Int
eval (Lit n) = n
eval (Add e1 e2) = (eval e1) + (eval e2)
eval (Sub e1 e2) = (eval e1) - (eval e2)
eval (Mul e1 e2) = (eval e1) * (eval e2)
eval (Div e1 e2) = (eval e1) `div` (eval e2)

-- show :: Expr -> String
instance Show Expr where
 show (Lit n) = show n
 show (Add e1 e2) = "(" ++ show e1 ++ "+" ++ show e2 ++ ")"
 show (Sub e1 e2) = "(" ++ show e1 ++ "-" ++ show e2 ++ ")"
 show (Mul e1 e2) = "(" ++ show e1 ++ "*" ++ show e2 ++ ")"
 show (Div e1 e2) = "(" ++ show e1 ++ "/" ++ show e2 ++ ")"

size :: Expr -> Int
size (Lit n) = 0
size (Add e1 e2) = 1 + size e1 + size e2
size (Sub e1 e2) = 1 + size e1 + size e2
size (Mul e1 e2) = 1 + size e1 + size e2
size (Div e1 e2) = 1 + size e1 + size e2

-- bij een deeling door nul krijg je de volgende error:
-- Program error: divide by zero

-- Opgave 14.X

-- voorbeeld data:
mobiel1 = (Arm 3 (Arm 4 (Gewicht 2) 2 (Gewicht 4)) 6 (Gewicht 3))
mobiel2 = (Arm 1 (Arm 3 (Arm 1 (Gewicht 2) 2 (Gewicht 1)) 2 (Gewicht 5)) 4 (Gewicht 2))

-- a)

data Mobiel = Arm Int Mobiel Int Mobiel | Gewicht Int

-- b)

gewicht :: Mobiel -> Int
gewicht (Gewicht n) = n
gewicht (Arm al ml ar mr) = gewicht ml + gewicht mr

-- c)
inEvenwicht :: Mobiel -> Bool
inEvenwicht (Gewicht n) = True
inEvenwicht (Arm al ml ar mr) = al * gewicht ml == ar * gewicht mr

-- d)

instance Show Mobiel where
 show (Gewicht n) = "(" ++ (show n) ++ ")"
 show (Arm al ml ar mr) = "[" ++ (show ml) ++ "]--" ++ (show al) ++ "--|--" ++ (show ar) ++ "--[" ++ (show mr) ++ "]"

instance Eq Mobiel where
 (Gewicht n1) == (Gewicht n2) = n1 == n2
 (Gewicht n) == (Arm al ml ar mr) = False
 (Arm al ml ar mr) == (Gewicht n1) = False
 (Arm al1 ml1 ar1 mr1) == (Arm al2 ml2 ar2 mr2) =
    ((al1 == al2 && ar1 == ar2 && ml1 == ml2 && mr1 == mr2) ||
    (al1 == ar2 && ar1 == al2 && ml1 == mr2 && mr1 == ml2))

instance Ord Mobiel where
 compare m1 m2 = compare (gewicht m1) (gewicht m2)