シリーズ: haskell
haskell
43 行
· 更新日 2026-02-03
exercise06.hs
haskell/exercise06.hs
module GPACalculator
( Grade(..)
, Mark, GPA
, markToGradeSafe
, gradeToGPA
, maybeGradeToGPA
, markToGPA
) where
-- ===== From earlier exercises =====
data Grade = Fail | Pass | Credit | Distinction | HighDistinction
deriving (Show, Eq, Ord, Enum, Bounded)
type Mark = Int
type GPA = Double
-- Safe mark->grade (Exercise 5)
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
-- ===== Exercise 6 =====
-- Grade -> GPA table (ANU-style)
gradeToGPA :: Grade -> GPA
gradeToGPA HighDistinction = 7
gradeToGPA Distinction = 6
gradeToGPA Credit = 5
gradeToGPA Pass = 4
gradeToGPA Fail = 0
-- Maybe Grade -> GPA (Nothing treated as 0)
maybeGradeToGPA :: Maybe Grade -> GPA
maybeGradeToGPA (Just g) = gradeToGPA g
maybeGradeToGPA Nothing = 0
-- Convenience: Mark -> GPA using the safe pipeline
markToGPA :: Mark -> GPA
markToGPA = maybeGradeToGPA . markToGradeSafe
関連記事
haskell
haskell
更新日 2026-02-03
Adding.hs
Adding.hs — haskell source code from the haskell learning materials (haskell/Adding.hs).
記事を読む →
haskell
haskell
更新日 2026-02-03
Area.hs
Area.hs — haskell source code from the haskell learning materials (haskell/Area.hs).
記事を読む →
haskell
haskell
更新日 2026-02-03
Chess.hs
Chess.hs — haskell source code from the haskell learning materials (haskell/Chess.hs).
記事を読む →
haskell
haskell
更新日 2026-02-03
ChessGraphics.hs
ChessGraphics.hs — haskell source code from the haskell learning materials (haskell/ChessGraphics.hs).
記事を読む →
haskell
haskell
更新日 2026-02-03
Examples.hs
Examples.hs — haskell source code from the haskell learning materials (haskell/Examples.hs).
記事を読む →
haskell
haskell
更新日 2026-02-03
Fibonacci.hs
Fibonacci.hs — haskell source code from the haskell learning materials (haskell/Fibonacci.hs).
記事を読む →