hw1: impl add, subtract, eq
This commit is contained in:
@@ -28,7 +28,14 @@ bigAdd :: BigNum -> BigNum -> BigNum
|
||||
bigAdd x y = bigAdd' x y 0
|
||||
|
||||
bigAdd' :: BigNum -> BigNum -> Block -> BigNum
|
||||
bigAdd' _ _ _ = error "Your code here"
|
||||
bigAdd' [] [] 0 = []
|
||||
bigAdd' [] [] c = [c]
|
||||
bigAdd' (x:xs) (y:ys) c = let s = x + y + c
|
||||
in (s `mod` maxblock) : bigAdd' xs ys (s `div` maxblock)
|
||||
bigAdd' (x:xs) [] c = let s = x + c
|
||||
in (s `mod` maxblock) : bigAdd' xs [] (s `div` maxblock)
|
||||
bigAdd' [] (y:ys) c = let s = y + c
|
||||
in (s `mod` maxblock) : bigAdd' [] ys (s `div` maxblock)
|
||||
|
||||
bigSubtract :: BigNum -> BigNum -> BigNum
|
||||
bigSubtract x y =
|
||||
@@ -44,10 +51,19 @@ stripLeadingZeroes xs = xs
|
||||
|
||||
-- Negative numbers are not supported, so you may throw an error in these cases
|
||||
bigSubtract' :: BigNum -> BigNum -> Block -> BigNum
|
||||
bigSubtract' _ _ _ = error "Your code here"
|
||||
bigSubtract' [] [] 0 = []
|
||||
bigSubtract' [] _ _ = error "Negative numbers not supported"
|
||||
bigSubtract' (x:xs) (y:ys) b = let d = x - y - b
|
||||
in if d < 0
|
||||
then (d + maxblock) : bigSubtract' xs ys 1
|
||||
else d : bigSubtract' xs ys 0
|
||||
bigSubtract' (x:xs) [] b = let d = x - b
|
||||
in if d < 0
|
||||
then (d + maxblock) : bigSubtract' xs [] 1
|
||||
else d : bigSubtract' xs [] 0
|
||||
|
||||
bigEq :: BigNum -> BigNum -> Bool
|
||||
bigEq _ _ = error "Your code here"
|
||||
bigEq x y = stripLeadingZeroes x == stripLeadingZeroes y
|
||||
|
||||
bigDec :: BigNum -> BigNum
|
||||
bigDec x = bigSubtract x [1]
|
||||
|
||||
13
hw1/output
13
hw1/output
@@ -0,0 +1,13 @@
|
||||
Addition
|
||||
[455,1]
|
||||
[455,3]
|
||||
[455,235,681]
|
||||
[455,235,681]
|
||||
[455,1,681]
|
||||
Subtraction
|
||||
[999]
|
||||
[962,634,9]
|
||||
[1]
|
||||
[0]
|
||||
Multiplication
|
||||
7
|
||||
|
||||
Reference in New Issue
Block a user