-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExNat.hs
146 lines (108 loc) · 2.63 KB
/
ExNat.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
module ExNat where
-- Do not alter this import!
import Prelude
( Show(..)
, Eq(..)
, Ord(..)
, Num(..)
, Integral
, Bool(..)
, not
, (&&)
, (||)
, (++)
, ($)
, (.)
, undefined
, error
, otherwise
)
-- Define evenerything that is undefined,
-- without using standard Haskell functions.
-- (Hint: recursion is your friend!)
data Nat = Zero | Succ Nat
deriving ( Eq )
data NatErro = Nat | Erro
deriving ( Eq )
instance Show Nat where
show Zero = "O"
show (Succ n) = "S" ++ show n
-- instance Eq Nat where
--
-- (==) = undefined
instance Ord Nat where
(<=) = undefined
-- Ord does not REQUIRE defining min and max.
-- Howevener, you should define them WITHOUT using (<=).
-- Both are binary functions: max m n = ..., etc.
min = undefined
max = undefined
isZero :: Nat -> Bool
isZero = undefined
-- pred is the predecessor but we define zero's to be zero
pred :: Nat -> Nat
pred = undefined
even :: Nat -> Bool
even = undefined
odd :: Nat -> Bool
odd = undefined
-- addition
(<+>) :: Nat -> Nat -> Nat
n <+> Zero = n
n <+> (Succ m) = Succ (n <+> m)
-- This is called the dotminus or monus operator
-- (also: proper subtraction, arithmetic subtraction, ...).
-- It behaves like subtraction, except that it returns 0
-- when "normal" subtraction would return a negative number.
(<->) :: Nat -> Nat -> Nat
n <-> Zero = n
Zero <-> n = Zero
(Succ n) <-> (Succ m) = n <-> m
-- multiplication
(<*>) :: Nat -> Nat -> Nat
(<*>) = undefined
-- exponentiation
(<^>) :: Nat -> Nat -> Nat
(<^>) = undefined
-- quotient
(</>) :: Nat -> Nat -> Nat
(</>) = undefined
-- remainder
(<%>) :: Nat -> Nat -> Nat
(<%>) = undefined
-- divides
(<|>) :: Nat -> Nat -> Bool
(<|>) = undefined
divides = (<|>)
-- x `absDiff` y = |x - y|
-- (Careful here: this - is the real minus operator!)
absDiff :: Nat -> Nat -> Nat
absDiff = undefined
(|-|) = absDiff
factorial :: Nat -> Nat
factorial = undefined
-- signum of a number (-1, 0, or 1)
sg :: Nat -> Nat
sg = undefined
-- lo b a is the floor of the logarithm base b of a
lo :: Nat -> Nat -> Nat
lo = undefined
--
-- For the following functions we need Num(..).
-- Do NOT use the following functions in the definitions above!
--
toNat :: Integral a => a -> Nat
toNat = undefined
fromNat :: Integral a => Nat -> a
fromNat = undefined
-- Obs: we can now easily make Nat an instance of Num.
instance Num Nat where
(+) = (<+>)
(*) = (<*>)
(-) = (<->)
abs n = n
signum = sg
fromInteger x
| x < 0 = undefined
| x == 0 = undefined
| otherwise = undefined