commit 14461fa7ef4a4e2c3f1d4418b9975eee878b3fab Author: Yuri Tatishchev Date: Sun Feb 1 22:33:08 2026 -0800 initial commit diff --git a/lab01/.gitignore b/lab01/.gitignore new file mode 100644 index 0000000..c4c4ffc --- /dev/null +++ b/lab01/.gitignore @@ -0,0 +1 @@ +*.zip diff --git a/lab01/fizzbuzz.hs b/lab01/fizzbuzz.hs new file mode 100644 index 0000000..8fb2cd3 --- /dev/null +++ b/lab01/fizzbuzz.hs @@ -0,0 +1,24 @@ +-- Do the game fizzbuzz (http://en.wikipedia.org/wiki/Fizz_buzz). +-- Return a string counting from 1 to the specified number. +-- Replace numbers dvisible by 3 with "fizz" and numbers divisible +-- by 5 with "buzz". If a number is divisible by both 3 and 5, +-- replace it with "fizzbuzz". + +fb :: Int -> String +fb x | mod x 3 == 0 && mod x 5 == 0 = "fizzbuzz" +fb x | mod x 3 == 0 = "fizz" +fb x | mod x 5 == 0 = "buzz" +fb x = show x + +fizzbuzz :: Int -> String +fizzbuzz n + | n < 0 = error "Non-negative numbers only" + | otherwise = unwords $ map fb [1..n] + +main :: IO () +main = do + print (fizzbuzz 1) + print (fizzbuzz 7) + print $ fizzbuzz 99 + print $ fizzbuzz 0 + print $ fizzbuzz (-2) diff --git a/lab01/lab1.txt b/lab01/lab1.txt new file mode 100644 index 0000000..96360af --- /dev/null +++ b/lab01/lab1.txt @@ -0,0 +1,38 @@ +NOTE: For all assignments, you are not allowed to modify the type signatures of the functions. + +If you do not have access to Canvas yet, please email me your solutions instead (thomas.austin@sjsu.edu). + +Part 1) +Navigate to http://codecheck.it/files/1801270115e7z5z5wxbxeqdep0nhqg29hic. +Implement the maxNum function. This function reads in a list of numbers and returns the largest. Define this function in a recursive manner. (Note that using the max function is cheating). + +Once you have passed all tests, click the "Download Report" button. + + +Part 2) +Implement the "fizzbuzz" game. The function counts from 1 to the specified number, returning a string with the result. The rules are: + + If a number is divisible by 3 and by 5, instead say "fizzbuzz" + Else if a number is divisible by 3, instead say "fizz" + Else if a number is divisible by 5, instead say "buzz" + Otherwise say the number + +Here is a sample run of this function: + +*Main> fizzbuzz 15 +"1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz" + +Go to http://codecheck.it/files/1801222202d5zry2pb674sm51n6sc7ajplp to work on the problem. + + +Part 3) +Go to http://codecheck.it/files/1801270120a4iwu1xmznyiyliwjqj9akxcl. + +In JSON.hs, implement the JObject case of the toString function. + + + +SUBMITTING YOUR WORK + +Once you have completed all three problems, you should have generated three different .zip files from CodeCheck. Zip those .zip files together, and upload the resulting .zip file to Canvas. + diff --git a/lab01/max.hs b/lab01/max.hs new file mode 100644 index 0000000..cb79b28 --- /dev/null +++ b/lab01/max.hs @@ -0,0 +1,14 @@ +maxNum :: [Integer] -> Integer + +maxNum [] = error "empty list" +maxNum [x] = x +maxNum (x:xs) = let m = maxNum xs in if x > m then x else m + +main :: IO () +main = do + print (maxNum [1,2,3]) + print (maxNum [4]) + print $ maxNum [7, 12, 0, 99, 46, 12349, 82] -- note the use of the '$' here + print $ maxNum [-4, -1, -99, -2] + print $ maxNum [7, -9, 12, 0, 99, -3, 46, 12349, 82, -7, 8] + print $ maxNum []