This file contains 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 (foldl', zipWith4) | |
lcs :: (Eq a) => [a] -> [a] -> [a] | |
lcs as bs = reverse . snd . last $ foldl' byRow ((0::Int, []):[(0, []) | _ <- as]) bs where | |
byRow m11s@(empty:m10s) b = scanl byCol empty (zip3 m11s m10s as) where | |
byCol m01@(m01_l, _) ((m11_l, m11_str), m10@(m10_l, _), a) | |
| a == b = (m11_l + 1, a : m11_str) | |
| otherwise = if m01_l > m10_l then m01 else m10 | |
-- Here we use zipWith4 instead of scanl. It's faster by 20-50 percent. |
This file contains 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.Map (fromList, (!)) | |
romans = zip (words "M CM D CD C XC L XL X IX V IV I") [1000,900,500,400,100,90,50,40,10,9,5,4,1] | |
romMap = fromList romans | |
toRoman = go romans where | |
go _ 0 = "" | |
go a@((r, c):rs) n | c <= n = r ++ go a (n - c) | |
| otherwise = go rs n | |
This file contains 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.Array.Unboxed | |
import Data.Array.Base (unsafeAt) | |
import Data.List.Split | |
import Data.List | |
type Table = UArray Int Char | |
solve :: String -> [String] | |
solve = map elems . go 0 . listArray (0, 80) where | |
This file contains 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 TupleSections, RecordWildCards #-} | |
import Prelude hiding (lookup) | |
import Control.Arrow (first) | |
data TTree k v = TTree {val :: !(Maybe v), node :: !(TNode k v)} deriving Show | |
data TNode k v = Empty | Node {key :: !k, lch, eqch, rch :: !(TTree k v)} deriving Show | |
empty :: TTree k v | |
empty = TTree Nothing Empty |
This file contains 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.Function (on) | |
data Leftist a = Empty | Node {_rank :: Int, val :: a, lch, rch :: (Leftist a)} | |
rank :: Leftist a -> Int | |
rank (Node r _ _ _) = r | |
rank _ = 0 | |
singleton :: a -> Leftist a | |
singleton a = Node 1 a Empty Empty |
This file contains 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 TupleSections, DeriveTraversable, DeriveFoldable, DeriveFunctor #-} | |
import Control.Monad.State | |
import Data.Traversable | |
import Data.Foldable | |
import qualified Data.Map as M | |
data Tree a = Empty | Tree a (Tree a) (Tree a) | |
deriving (Show, Functor, Foldable, Traversable) |
This file contains 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 qualified Data.Traversable as T | |
import Control.Monad.Cont | |
data Zipper t a = ZDone (t a) | Zipper a (Maybe a -> Zipper t a) | |
data BiZip t a = BiZip [Maybe a] -- edits we've done on the left | |
[Maybe a] -- edits we've done on the right | |
[Zipper t a] -- saved Zippers from the left edits | |
mkZipper :: T.Traversable t => t a -> Zipper t a | |
mkZipper t = (`runCont` id) $ T.mapM yield t >>= return . ZDone |
This file contains 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 Control.Monad | |
import Control.Concurrent | |
import Control.Concurrent.STM | |
import System.Random | |
import Text.Printf | |
philosophers = ["Aristotle", "Kant", "Spinoza", "Marx", "Russel"] | |
waitSome = randomRIO (1000, 5000) >>= threadDelay . (*1000) |
This file contains 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 Text.Printf | |
import Control.Monad.State.Strict | |
import qualified Data.HashMap.Strict as HM | |
data OptType = Call | Put deriving (Show, Enum) | |
data Continent = Am | Eur deriving (Show, Enum) | |
optPrice optType continent s k sigma rf dt n divPayments = let | |
u = exp (sigma * sqrt dt) | |
d = 1 / u |
This file contains 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 qualified Data.Set as S | |
import qualified Data.Map as M | |
import Control.Monad | |
import Data.Bool | |
step :: S.Set (Int, Int) -> S.Set (Int, Int) | |
step grid = let | |
neighs (i, j) = [((i + di, j + dj), 1) | | |
(di, dj) <- tail $ liftM2 (,) [0, -1, 1] [0, -1, 1]] | |
freqs = M.fromListWith (+) $ neighs =<< S.toList grid |
OlderNewer