Last active
December 14, 2015 18:09
-
-
Save rinx/5127112 to your computer and use it in GitHub Desktop.
GPA計算するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- <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