All pastes #201046 Raw Edit

Miscellany

public text v1 · immutable
#201046 ·published 2006-10-13 15:42 UTC
rendered paste body
module Overload where

--12.8
--instance (Ord a, Ord b) => Ord (a,b) where
 

--instance Ord b => Ord [b] where 
 --Ord b = Ord [b]

--14.16 + 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) = div (eval e1) (eval 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)

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 ++ ")"

--Example: size (Add (Mul (Lit 2) (Lit 2)) (Sub (Lit 6) (Lit 3)))

--14.X
--a
data Mobiel	= Gewicht Int | Arm Mobiel Int Mobiel Int

--b
gewicht :: Mobiel -> Int
gewicht (Gewicht x) = x
gewicht (Arm g1 m1 g2 m2) = gewicht g1 + gewicht g2

-- example mob1: gewicht ( Arm ( Arm (Gewicht 2) 1 (Gewicht 4) 1 ) 1 (Gewicht 3) 1)
-- example mob2: gewicht ( Arm ( Arm ( Arm (Gewicht 2) 1 (Gewicht 1) 1) 1 (Gewicht 5) 1 ) 1 (Gewicht 2) 1 )

--c
gelijkMoment :: Mobiel -> Int -> Mobiel -> Int -> Bool
gelijkMoment links m1 rechts m2 = gewicht links * m1 == gewicht rechts * m2


inEvenwicht :: Mobiel -> Bool
inEvenwicht (Arm links m1 rechts m2) = gelijkMoment links m1 rechts m2
inEvenwicht (Arm links m1 rechts m2) = gelijkMoment links m1 rechts m2 && inEvenwicht links && inEvenwicht rechts

--example1: inEvenwicht ( Arm ( Arm (Gewicht 2) 4 (Gewicht 4) 2 ) 3 (Gewicht 3) 6)
--example2: inEvenwicht ( Arm ( Arm ( Arm (Gewicht 2) 1 (Gewicht 1) 1) 1 (Gewicht 5) 1 ) 1 (Gewicht 2) 1 )

--d
instance Show Mobiel where
 show (Gewicht n) = show n
 show (Arm links m1 rechts m2) = "(Linkerarm " ++ show links ++ " met lengte " ++ show m1 ++ " en rechterarm " ++ show rechts ++ " met lengte " ++ show m2 ++ ")"

instance Eq Mobiel where
 (Gewicht n1) == (Gewicht n2) = n1 == n2
--laatste ff van joris gecopied om te testen