Skip to content

Instantly share code, notes, and snippets.

@5outh
5outh / FracFrac.hs
Created August 15, 2012 22:21
FracFrac
instance Fractional Fraction where
(/) f = (*) f . recip
recip (Frac a b) = Frac b a
fromRational r = Frac (numerator r) (denominator r)
@5outh
5outh / EqOrd.hs
Created August 15, 2012 22:37
EqOrd
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
@5outh
5outh / MonoidConstructor.hs
Created August 15, 2012 22:47
MonoidConstructor
instance Monoid Fraction where
mempty = 0
mappend = (+)
mconcat = foldr mappend mempty
(%) :: Integer -> Integer -> Fraction
(%) a = simplify . Frac a
@5outh
5outh / Euler057.hs
Created August 15, 2012 23:07
Euler057
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) )
@5outh
5outh / gist:3364744
Created August 15, 2012 23:34
Fraction
module Fraction
(
Fraction,
(%),
simplify,
num,
denom
) where
import Data.Monoid
@5outh
5outh / tsort.hs
Created December 5, 2012 22:04
tsort
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
@5outh
5outh / alltogether.hs
Created December 5, 2012 22:05
alltogether
danceOutcome = graphFromFile "people.txt" >>= \f -> return $ tsort f
@5outh
5outh / Graph.hs
Created December 5, 2012 22:06
Graph
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
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)]
Leslie Andy
April Andy
Ron Ann
Ron April
Ann Jerry
Ann Andy
Leslie April
Ron Andy
Leslie Ron
Andy Jerry