bavardage (owner)

Revisions

gist: 225290 Download_button fork
public
Public Clone URL: git://gist.github.com/225290.git
Embed All Files: show embed
typesandstuff.hs #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
type Vector a = [a]
 
 
data Shape = Square Float | Rectangle Float Float | Circle Float
 
area :: Shape -> Float
area (Square x) = x * x
area (Rectangle a b) = a * b
area (Circle r) = pi * r * r
 
 
instance Show Shape where
    show (Square x) = "A square of side length " ++ show x
 
 
data Colour = Brown | Blond | Black | Grey deriving Show
data Person = Person String Int Colour | BaldPerson String Int deriving Show
 
 
 
name :: Person -> String
name (Person n _ _) = n
name (BaldPerson n _) = n
age (Person _ a _ ) = a
age (BaldPerson _ a) = a
hairColour (Person _ _ c) = c
 
data BadPerson = BadPerson { name' :: String
                           , age' :: Int
                           , hairColour' :: Colour } deriving Show
 
 
 
data Mod5 = Mod5 Int deriving Show
 
instance Eq Mod5 where
    (Mod5 a) == (Mod5 b) = (a `mod` 5) == (b `mod` 5)
 
instance Num Mod5 where
    (Mod5 a) + (Mod5 b) = Mod5 $ (a + b) `mod` 5
    (Mod5 a) * (Mod5 b) = Mod5 $ (a * b) `mod` 5
    (Mod5 a) - (Mod5 b) = Mod5 $ (a - b) `mod` 5
    abs = id
    signum 0 = 0
    signum _ = 1
    fromInteger x = Mod5 ((fromInteger x) `mod` 5)