initial commit

This commit is contained in:
2026-02-01 22:33:08 -08:00
commit 14461fa7ef
4 changed files with 77 additions and 0 deletions

24
lab01/fizzbuzz.hs Normal file
View File

@@ -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)