Série: haskell
haskell
53 linhas
· Atualizado 2026-02-03
exercise04.hs
haskell/exercise04.hs
module GPACalculator
( Grade(..)
, Mark
, Course(..)
, markToGrade
, markToGradeSafe
, markToGrade' -- Exercise 4
, markToGrade'Safe -- (optional safe version)
) where
-- ===== Exercise 3 bits (kept here so this file is standalone) =====
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
markToGrade :: Mark -> Grade
markToGrade m
| m < 0 || m > 100 = error "markToGrade: mark must be between 0 and 100"
| m >= 80 = HighDistinction
| m >= 70 = Distinction
| m >= 60 = Credit
| m >= 50 = Pass
| otherwise = Fail
markToGradeSafe :: Mark -> Maybe Grade
markToGradeSafe m
| m < 0 || m > 100 = Nothing
| otherwise = Just (markToGrade m)
-- ===== Exercise 4 =====
-- Course identified by a code in each school
data Course
= Art Int
| Comp Int
| Busn Int
| Math Int
deriving (Show, Eq, Ord)
-- Ignore the course for grading; grade depends only on the mark.
markToGrade' :: (Course, Mark) -> Grade
markToGrade' (_, m) = markToGrade m
-- Safe version that never throws
markToGrade'Safe :: (Course, Mark) -> Maybe Grade
markToGrade'Safe (_, m) = markToGradeSafe m
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 →