Created
July 9, 2011 05:22
-
-
Save o-sam-o/1073350 to your computer and use it in GitHub Desktop.
Haskell Hack
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
import Data.Char | |
import Data.Maybe | |
import Data.List | |
import Data.Map hiding (map) | |
import Text.Printf | |
main = do | |
src <- readFile "grades.txt" | |
let pairs = map (split.words) (lines src) | |
let grades = foldr insert empty pairs | |
mapM_ (draw grades) (sort (keys grades)) | |
where | |
insert (s, g) = insertWith (++) s [g] | |
split [name,mark] = (name, read mark) | |
draw g s = printf "%s\t%s\tAverage: %f\n" s (show marks) avg | |
where | |
marks = findWithDefault (error "No such student") s g | |
avg = sum marks / fromIntegral (length marks) :: Double |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment