hw1: init

This commit is contained in:
2026-02-14 19:57:27 -08:00
parent 52e7e119b6
commit 09f11f0e8b
9 changed files with 281 additions and 0 deletions

89
hw1/BigNum.hs Normal file
View File

@@ -0,0 +1,89 @@
{-
Name: <Your name here>
Class: CS 252
Assigment: HW1
Date: <Date assignment is due>
Description: <Describe the program and what it does>
-}
module BigNum (
BigNum,
bigAdd,
bigSubtract,
bigMultiply,
bigEq,
bigDec,
bigPowerOf,
prettyPrint,
stringToBigNum,
) where
type Block = Int -- An Int from 0-999
type BigNum = [Block]
maxblock = 1000
bigAdd :: BigNum -> BigNum -> BigNum
bigAdd x y = bigAdd' x y 0
bigAdd' :: BigNum -> BigNum -> Block -> BigNum
bigAdd' _ _ _ = error "Your code here"
bigSubtract :: BigNum -> BigNum -> BigNum
bigSubtract x y =
if length x < length y
then error "Negative numbers not supported"
else reverse $ stripLeadingZeroes $ reverse result
where result = bigSubtract' x y 0
stripLeadingZeroes :: BigNum -> BigNum
stripLeadingZeroes (0:[]) = [0]
stripLeadingZeroes (0:xs) = stripLeadingZeroes xs
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"
bigEq :: BigNum -> BigNum -> Bool
bigEq _ _ = error "Your code here"
bigDec :: BigNum -> BigNum
bigDec x = bigSubtract x [1]
-- Handle multiplication following the same approach you learned in grade
-- school, except dealing with blocks of 3 digits rather than single digits.
-- If you are having trouble finding a solution, write a helper method that
-- multiplies a BigNum by an Int.
bigMultiply :: BigNum -> BigNum -> BigNum
bigMultiply _ _ = error "Your code here"
bigPowerOf :: BigNum -> BigNum -> BigNum
bigPowerOf _ _ = error "Your code here"
prettyPrint :: BigNum -> String
prettyPrint [] = ""
prettyPrint xs = show first ++ prettyPrint' rest
where (first:rest) = reverse xs
prettyPrint' :: BigNum -> String
prettyPrint' [] = ""
prettyPrint' (x:xs) = prettyPrintBlock x ++ prettyPrint' xs
prettyPrintBlock :: Int -> String
prettyPrintBlock x | x < 10 = ",00" ++ show x
| x < 100 = ",0" ++ show x
| otherwise = "," ++ show x
stringToBigNum :: String -> BigNum
stringToBigNum "0" = [0]
stringToBigNum s = stringToBigNum' $ reverse s
stringToBigNum' :: String -> BigNum
stringToBigNum' [] = []
stringToBigNum' s | length s <= 3 = read (reverse s) : []
stringToBigNum' (a:b:c:rest) = block : stringToBigNum' rest
where block = read $ c:b:a:[]
sig = "9102llaf"