Skip to content

Instantly share code, notes, and snippets.

View kseo's full-sized avatar

Kwang Yul Seo kseo

  • CodeChain
  • Seoul
View GitHub Profile
@kseo
kseo / IntMap.hs
Created February 14, 2014 04:35
A Haskell implementation of Okasaki and Gill's Fast Mergeable Integer Maps
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
@kseo
kseo / WCEnumerator.hs
Last active December 31, 2015 19:09
wc written in Haskell using enumerator
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
@kseo
kseo / FindEnumerator.hs
Last active December 31, 2015 19:49
find written in Haskell
-- 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
@kseo
kseo / Reduce.hs
Created January 27, 2016 02:40
Reduce
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 (:)
@kseo
kseo / SCFold.hs
Created February 2, 2016 09:09
Short-Circuiting Fold
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
@kseo
kseo / Length.hs
Created February 2, 2016 15:13
An implementation of length function in terms of Const functor
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))
@kseo
kseo / Nested.hs
Created February 11, 2016 11:01
Nested types
{-# 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
@kseo
kseo / Polymorphic.hs
Created February 11, 2016 11:23
Polymorphic recursion
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
@kseo
kseo / Seq.hs
Created February 11, 2016 13:53
Sequence implementation using Monoid
-- 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)
@kseo
kseo / MonadComprehension.hs
Created February 12, 2016 04:07
Monad comprehension
-- 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]