From 54bf0bfd56c5dc0d3807eeb3d14390e77d1066bd Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Sat, 14 Feb 2026 23:21:37 -0800 Subject: [PATCH] hw1: impl add, subtract, eq --- hw1/BigNum.hs | 22 +++++++++++++++++++--- hw1/output | 13 +++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/hw1/BigNum.hs b/hw1/BigNum.hs index 3de9087..d4cc234 100644 --- a/hw1/BigNum.hs +++ b/hw1/BigNum.hs @@ -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] diff --git a/hw1/output b/hw1/output index e69de29..42db385 100644 --- a/hw1/output +++ b/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