lab05: impl

This commit is contained in:
2026-02-21 21:28:52 -08:00
parent 5138e3f817
commit c481875eca
2 changed files with 25 additions and 0 deletions

25
lab05/maybeEither.hs Normal file
View File

@@ -0,0 +1,25 @@
getMax :: [Int] -> Maybe Int
getMax [] = Nothing
getMax x = Just (maximum x)
reciprocal :: (Eq a, Fractional a) => a -> Maybe a
reciprocal 0 = Nothing
reciprocal x = Just (1/x)
rectangleArea :: Int -> Int -> Either String Int
rectangleArea x y
| x < 0 = Left "Width is not positive"
| y < 0 = Left "Height is not positive"
| otherwise = Right (x * y)
main :: IO ()
main = do
print $ getMax []
print $ getMax [99,12,37]
print $ getMax [-99,-12,-37]
print $ reciprocal 4
print $ reciprocal 2
print $ reciprocal 0
print $ rectangleArea 5 10
print $ rectangleArea (-5) 10
print $ rectangleArea 5 (-10)

Binary file not shown.