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
instance Eq Fraction where | |
(/=) f = not . (==) f | |
(==) f f' = (x == x') && (y == y') | |
where (Frac x y) = simplify f | |
(Frac x' y') = simplify f' | |
instance Ord Fraction where | |
compare (Frac a b) (Frac c d) = compare (a `quot` b) (c `quot` d) | |
(<) f = (==) LT . compare f | |
(>) f = (==) GT . compare f |
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
instance Monoid Fraction where | |
mempty = 0 | |
mappend = (+) | |
mconcat = foldr mappend mempty | |
(%) :: Integer -> Integer -> Fraction | |
(%) a = simplify . Frac a |
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 Fraction | |
main = return . length . filter moreInNum . map sqrtTwo $ [1..1000] | |
where moreInNum f = length ( (show . num) f ) > length ( (show . denom) f) | |
sqrtTwo = simplify . (+) 1 . sqrtTwo' | |
where sqrtTwo' 1 = 1 % 2 | |
sqrtTwo' n = 1 / ( 2 + sqrtTwo' (pred n) ) |
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
module Fraction | |
( | |
Fraction, | |
(%), | |
simplify, | |
num, | |
denom | |
) where | |
import Data.Monoid |
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.List --for later | |
import System.Environment --for later | |
import Graph | |
data Letter = A | B | C | D | E | F deriving (Show, Eq, Enum) | |
sample :: Graph Letter | |
sample = Graph [A,B,C,D,E,F] [(A, B), (A, C), (B, D), (C, D), (D, E), (D, F), (B, C), (F, E)] |
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
graphFromFile :: String -> IO (Graph String) | |
graphFromFile f = do | |
contents <- readFile f | |
let info = map words $ lines contents | |
verts = nub . concat $ info | |
conns = map (\[a, b] -> (a, b)) info | |
graph = Graph verts conns | |
return graph |
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
tsort :: (Eq a) => Graph a -> [a] | |
tsort graph = tsort' [] (noInbound graph) graph | |
where noInbound (Graph v e) = filter (flip notElem $ map snd e) v | |
tsort' l [] (Graph _ []) = reverse l | |
tsort' l [] _ = error "There is at least one cycle in this graph." | |
tsort' l (n:s) g = tsort' (n:l) s' g' | |
where outEdges = outbound n g | |
outNodes = map snd outEdges | |
g' = foldr removeEdge g outEdges | |
s' = s ++ filter (null . flip inbound g') outNodes |
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
danceOutcome = graphFromFile "people.txt" >>= \f -> return $ tsort f |
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
module Graph( | |
Graph(Graph), | |
removeEdge, | |
outbound, | |
inbound | |
)where | |
data Graph a = Graph{ vertices :: [a], edges :: [(a, a)] } deriving Show | |
removeEdge :: (Eq a) => (a, a) -> Graph a -> Graph a |
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
Leslie Andy | |
April Andy | |
Ron Ann | |
Ron April | |
Ann Jerry | |
Ann Andy | |
Leslie April | |
Ron Andy | |
Leslie Ron | |
Andy Jerry |