Série: haskell
haskell
38 linhas
· Atualizado 2026-02-03
exercise05.hs
haskell/exercise05.hs
module GPACalculator
( Grade(..)
, Mark
, markToGrade -- throws on out-of-range (as in Ex. 3/4)
, markToGradeSafe -- Ex. 5: Maybe-based safe version
) where
data Grade
= Fail -- 0–49
| Pass -- 50–59
| Credit -- 60–69
| Distinction -- 70–79
| HighDistinction -- 80–100
deriving (Show, Eq, Ord, Enum, Bounded)
type Mark = Int
-- Original (kept for comparison): errors if mark is out of bounds
markToGrade :: Mark -> Grade
markToGrade m
| m < 0 || m > 100 = error "markToGrade: Not a valid mark"
| m >= 80 = HighDistinction
| m >= 70 = Distinction
| m >= 60 = Credit
| m >= 50 = Pass
| otherwise = Fail
-- Exercise 5: safe version using Maybe
-- Out-of-range -> Nothing
-- In-range -> Just <Grade>
markToGradeSafe :: Mark -> Maybe Grade
markToGradeSafe m
| m < 0 || m > 100 = Nothing
| m >= 80 = Just HighDistinction
| m >= 70 = Just Distinction
| m >= 60 = Just Credit
| m >= 50 = Just Pass
| otherwise = Just Fail
Artigos relacionados
haskell
haskell
Atualizado 2026-02-03
Adding.hs
Adding.hs — haskell source code from the haskell learning materials (haskell/Adding.hs).
Ler artigo →
haskell
haskell
Atualizado 2026-02-03
Area.hs
Area.hs — haskell source code from the haskell learning materials (haskell/Area.hs).
Ler artigo →
haskell
haskell
Atualizado 2026-02-03
Chess.hs
Chess.hs — haskell source code from the haskell learning materials (haskell/Chess.hs).
Ler artigo →
haskell
haskell
Atualizado 2026-02-03
ChessGraphics.hs
ChessGraphics.hs — haskell source code from the haskell learning materials (haskell/ChessGraphics.hs).
Ler artigo →
haskell
haskell
Atualizado 2026-02-03
Examples.hs
Examples.hs — haskell source code from the haskell learning materials (haskell/Examples.hs).
Ler artigo →
haskell
haskell
Atualizado 2026-02-03
Fibonacci.hs
Fibonacci.hs — haskell source code from the haskell learning materials (haskell/Fibonacci.hs).
Ler artigo →