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
module IntMap where | |
import Data.Bits | |
import Prelude hiding (lookup) | |
-- Fast Mergeable lnteger Maps* | |
-- http://ittc.ku.edu/~andygill/papers/IntMap98.pdf | |
-- Little-endian implementation | |
type Key = Int |
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.ByteString as BS | |
import qualified Data.ByteString.Internal as BS (c2w, w2c) | |
import Data.Enumerator hiding (head) | |
import qualified Data.Enumerator.Binary as EB | |
import qualified Data.Enumerator.List as EL | |
import System.Environment | |
countLines :: Iteratee BS.ByteString IO Int | |
countLines = do | |
bs <- EL.head |
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
-- From A tutorial on the enumerator library | |
-- http://www.mew.org/~kazu/proj/enumerator/ | |
import Control.Applicative | |
import Control.Monad | |
import Control.Monad.IO.Class | |
import Data.Enumerator hiding (map, filter, filterM) | |
import qualified Data.Enumerator.List as EL | |
import Data.List | |
import System.Directory | |
import System.FilePath |
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
class Reduce t where | |
reducer :: (a -> b -> b) -> (t a -> b -> b) | |
reducel :: (b -> a -> b) -> (b -> t a -> b) | |
instance Reduce [] where | |
reducer f x z = foldr f z x | |
reducel f x z = foldl f x z | |
toList :: (Reduce f) => f a -> [a] | |
toList s = s `cons` [] where cons = reducer (:) |
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.Maybe | |
ssfold :: (a -> Bool) -> (a -> b -> a) -> a -> [b] -> a | |
ssfold p f a0 xs = foldr (\x xs a -> if p a then a else xs (f a x)) id xs a0 |
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 Prelude hiding (length) | |
newtype Const a b = Const a | |
unConst :: Const c a -> c | |
unConst (Const x) = x | |
length' :: [a] -> Const Int a | |
length' [] = Const 0 | |
length' (x:xs) = Const (1 + unConst (length' xs)) |
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 FlexibleContexts #-} | |
data BinaryTree a = Leaf | |
| InternalNode (BinaryTree a) a (BinaryTree a) deriving Show | |
data CompleteBinaryTree a = Leaves | |
| NonLeaves a (CompleteBinaryTree (a,a)) deriving Show | |
data AList a b = Nil | |
| Cons a (AList b a) deriving Show |
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 Prelude hiding (length) | |
data Nested a = a :<: (Nested [a]) | Epsilon | |
infixr 5 :<: | |
nested = 1 :<: [2,3,4] :<: [[4,5],[7],[8,9]] :<: Epsilon | |
length :: Nested a -> Int | |
length Epsilon = 0 | |
length (_ :<: xs) = 1 + length xs |
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
-- http://apfelmus.nfshost.com/articles/monoid-fingertree.html | |
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
import Prelude hiding ((!!), head) | |
import Data.Monoid | |
data Tree v a = Leaf v a | |
| Branch v (Tree v a) (Tree v a) |
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
-- https://ghc.haskell.org/trac/ghc/wiki/MonadComprehensions | |
{-# LANGUAGE MonadComprehensions #-} | |
import Control.Monad | |
mapl :: Monad m => (a -> m b) -> ([a] -> m [b]) | |
mapl f [] = return [] | |
mapl f (x:xs) = [y:ys | y <- f x, ys <- mapl f xs] |
OlderNewer