-
-
Save axman6/1073412 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 :: IO () | |
main = do | |
(src :: String) <- readFile "grades.txt" :: IO String | |
let pairs = map (\x -> split (words x)) ((lines src) :: [String]) :: [(String,Int)] | |
let grades = foldr insert empty pairs :: Map String Int | |
mapM_ (draw grades) (sort (keys grades)) :: IO () | |
where | |
insert :: (String, Int) -> (Map String Int -> Map String Int) | |
insert (s, g) mp = insertWith (++) s [g] mp | |
split :: [String] -> (String, Int) | |
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 | |
double :: [Int] -> [Int] | |
double = map (*2) | |
foo :: a -> b -> c -> d | |
foo :: a -> (b -> (c -> d)) | |
foo (x :: a) :: b -> (c -> d) | |
foo :: Int -> Int -> Int -> Int | |
foo x y z = x^y+z | |
bar = foo 7 | |
bar y z = 7^y+z | |
baz = bar 12 | |
baz z = 7^12+z | |
map baz [2..7] | |
("foo",2.3) :: (String, Double) | |
foldr :: (a -> b -> b) -> b -> [a] -> b | |
data [a] = [] | a : [a] | |
data List a = Null | Cons a (List a) | |
[a,b,c] = a : (b : (c : [])) | |
= Cons a (Cons b (Cons c Null)) | |
a : (b : (c : [])) | |
foldr f z [a,b,c] = a `f` (b `f` (c `f` z)) | |
foldr (+) 0 [a,b,c] = a + (b + (c + 0)) | |
foldr (*) 1 [a,b,c] = a * (b * (c * 1)) | |
foldr (:) [] [a,b,c] = a : (b : (c : [])) -> [a,b,c] | |
foldr insert empty [a,b] = a `insert` (b `insert` empty) | |
True && x = x | |
False && x = False | |
foldr (&&) True [a,b,c] | |
= a && (b && (c && True)) | |
= True && (b && (c && True)) | |
= (b && (c && True)) | |
= False && (c && True) | |
= False | |
foldl f z (x:xs) = foldl f (f x z) xs | |
foldl f z [] = z | |
foldl f z [a,b,c] | |
= foldl f z (a : [b,c]) | |
= foldl f (f a z) [b,c] | |
= foldl f (f a z) (b : [c]) | |
= foldl f (f b (f a z)) [c] | |
= foldl f (f b (f a z)) (c:[]) | |
= foldl f (f c (f b (f a z))) [] | |
= f c (f b (f a z)) | |
foldl (+) 0 [1,2,3] | |
= foldl (+) ((+) 1 0) [2,3] | |
= foldl (+) (1 + 0) [2,3] | |
= foldl (+) 1 [2,3] | |
= foldl (+) (2 + 1) [3] | |
= foldl (+) 3 [3] | |
= foldl (+) (3 + 3) [] | |
= foldl (+) 6 [] | |
= 6 | |
par :: a -> b -> b | |
par a b | |
foo x y = x' `par` y' `par` (x' + y') | |
where x' = (somethingMassive x) | |
y' = (somethingMassive y) | |
parMap :: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment