Skip to content

Instantly share code, notes, and snippets.

@erantapaa
erantapaa / l-systems.lhs
Last active September 30, 2015 01:02
L-systems in Haskell
L-systems in Haskell.
===
> import Data.List.Split (splitOn)
> import Data.List
>
> type Rules = [(Char, String)]
>
> -- compute the next generation
> next :: Rules -> String -> String
@erantapaa
erantapaa / brotli.hs
Last active September 30, 2015 05:32
part of the brutal compression algorithm in Haskell
module Lib
where
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.ST
import qualified Data.Vector.Unboxed.Mutable as UVM
import qualified Data.Vector.Unboxed as UV
calculate_codes :: PrimMonad m => UV.Vector Int -> UV.Vector Int -> m (UV.Vector Int)
@erantapaa
erantapaa / bibparse.py
Last active October 14, 2021 20:44
BibTeX file parsing Python
#
# Simple BibTeX file parsing in python.
#
# See `bibtest1` for an example of usage.
#
# This is a good overview of how to correctly parse a bibtex file:
#
# http://maverick.inria.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html
import string
@erantapaa
erantapaa / matching.hs
Created October 2, 2015 06:59
Gale-Shapely in Haskell
{-# LANGUAGE FlexibleContexts #-}
--- Some experiments vis-a-vis the Gale-Shapely algorithm.
import Data.List
import Control.Monad
import Data.Array
import Data.Array.MArray
import Data.Array.ST
import Data.Ord
@erantapaa
erantapaa / exact-cover.md
Last active October 6, 2015 16:50
using set-exact

This is a quick tutorial on how to use the set-cover package to solve exact cover problems.

Exact Cover Problems

Given a set X and collection of subsets of X, S, the exact cover problem asks if there is a sub-collection S' of S s.t. the subsets in S' are pairwise disjoint and their union equals X.

@erantapaa
erantapaa / bowl.lhs
Last active November 8, 2015 13:26
using parsec to score a bowling game
Using Parsec to score a bowling game.
===
In this gist we'll see how to use Parsec to solve the problem of
scoring a bowling game. This was inspired by a
Reddit Daily Programmer problem:
https://www.reddit.com/r/dailyprogrammer/comments/3ntsni/20151007_challenge_235_intermediate_scoring_a/
@erantapaa
erantapaa / oralist-examples.lhs
Last active October 9, 2015 10:56
ordlist example
A gist collecting examples of how to use `Data.List.Ordered`
> import Data.List.Ordered
Sieve of Eratosthenes
===
From the Haskellwiki entry on the [Sieve of Eratosthenes][HaskellWikiSieve]:
@erantapaa
erantapaa / Timed.hs
Last active April 14, 2024 15:31
non-blocking I/O examples in Haskell
import System.Environment
import System.Timeout (timeout)
import Control.Concurrent
import Control.Concurrent (forkIO, threadDelay, killThread)
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
import System.Process
import System.IO
-- blocking IO
@erantapaa
erantapaa / pretty-html-conduit.hs
Created October 20, 2015 16:54
pretty printing HTML with Conduit
{-# LANGUAGE NoMonomorphismRestriction #-}
-- build-depends: base >= 4.7 && < 5, xml-conduit, text, conduit, transformers, resourcet, html-conduit, conduit-extra
module Lib
where
import qualified Text.XML.Stream.Parse as P
import qualified Text.XML.Stream.Render as R
import qualified Data.Conduit.List as CL
@erantapaa
erantapaa / pipes-file-example.hs
Created October 21, 2015 10:10
pipes-file example
import Pipes
import Pipes.Files
import Pipes.Safe
import Data.Monoid
import Data.Time.Clock
import System.Posix.Files.ByteString ( fileSize )
example = do
let megs = 1024 * 1024
days = 86400