Skip to content

Instantly share code, notes, and snippets.

@rinx
Last active December 14, 2015 18:09
Show Gist options
  • Save rinx/5127112 to your computer and use it in GitHub Desktop.
Save rinx/5127112 to your computer and use it in GitHub Desktop.
GPA計算するやつ
-- <example>
-- $runghc calcGPA.hs
-- 3
-- A 2
-- B 2
-- C 1
-- 2.2
import Control.Monad
import Control.Applicative
calcGPA :: [(String, Double)] -> Double
calcGPA xs = grades xs / addUnits xs
grades :: [(String, Double)] -> Double
grades [] = 0
grades (x:xs)
| fst x == "AA" = 4 * snd x + grades xs
| fst x == "A" = 3 * snd x + grades xs
| fst x == "B" = 2 * snd x + grades xs
| fst x == "C" = 1 * snd x + grades xs
| fst x == "D" = grades xs
| otherwise = 0
addUnits :: [(String, Double)] -> Double
addUnits = foldl (\acc x -> acc + snd x) 0
main = do
n <- getLine
s <- replicateM (read n) $ (\(x:y) -> (x, read (head y) :: Double)) . words <$> getLine
putStrLn . show $ calcGPA s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment