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
type Graph = Vector [Int] | |
data BfsState = BfsState { queue :: Seq [Int] | |
, visited :: Set Int } | |
pop :: MaybeT (State BfsState) [Int] | |
pop = do | |
(x :< xs) <- viewl <$> gets queue | |
modify $ \y -> y { queue = xs } | |
return x |
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
class Hashable a where | |
hash :: Bits b => a -> b | |
instance Hashable Int where | |
hash = id |
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
fib :: Int -> Int | |
fib = memo fib' | |
where fib' 0 = 1 | |
fib' 1 = 1 | |
fib' n = fib' (n - 1) + fib' (n - 2) |
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
{-# LANGUAGE ViewPatterns #-} | |
module Memo where | |
import Data.Bits (testBit, setBit, finiteBitSize) | |
data Memo a b = Fork (Memo a b) b (Memo a b) | |
deriving Show | |
type Bit = Bool | |
newtype Bits = Bits [Bit] |
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
{-# LANGUAGE ViewPatterns #-} | |
module Memo where | |
import Data.Bits (testBit, setBit, finiteBitSize) | |
data Memo a b = Fork (Memo a b) b (Memo a b) | |
deriving Show | |
type Bit = Bool | |
newtype Bits = Bits [Bit] |
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
{- | |
(x - xs)^2 + (y - ys)^2 + (z - zs)^2 = r^2 | |
x(t) = xr + t * xv | |
y(t) = yr + t * yv | |
z(t) = zr + t * zv | |
(xr - xs + t * xv)^2 + (yr - ys + t * yv)^2 + (zr - zs + t * zv)^2 = r^2 | |
t^2(xv^2 + yv^2 + zv^2) + 2t(xv(xr - xs) + yv(yr - ys) + zv(zr - zs)) + | |
(xr - xs)^2 + (yr - ys)^2 + (zr - zs)^2 - r^2 = 0 |
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
nubRuns :: Eq a => [a] -> [a] | |
nubRuns [] = [] | |
nubRuns (x : xs) = foldr (\x list -> if x == head list then list else x : list) [x] xs |
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
class Equal x y b | x y -> b | |
instance x ~ y => Equal x y True | |
instance Equal x y False |
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
class Equal x y b | x y -> b | |
instance Equal x x True | |
instance Not True b => Equal x y b |
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
class AList a | |
instance AList Nil | |
instance (A x, AList xs) => AList (Cons x xs) |