Skip to content

Instantly share code, notes, and snippets.

@lylek
lylek / pearls-ch6.hs
Last active February 18, 2018 23:52
Code from Chapter 6 of Pearls of Functional Algorithm Design
#!/usr/bin/env stack
-- stack --install-ghc runghc --package=criterion
import Criterion.Main
import Data.Char (intToDigit)
import Data.List (intercalate)
type Expression = [Term]
type Term = [Factor]
type Factor = [Digit]
-- Semigroups and monoids
-- On Maybe:
-- Nothing disappears, left wins
instance Monoid (Maybe a) where
mempty = Nothing
Nothing `mappend` y = y
x `mappend` _ = x
#!/usr/bin/env stack
-- stack --resolver lts-5.5 --install-ghc runghc
-- Pizza Hut solution for Pi Day problem 1
-- http://blog.pizzahut.com/flavor-news/national-pi-day-math-contest-problems-are-here-2/
import Data.List (permutations)
main = putStrLn $ head solutions
@lylek
lylek / SchemeParser.hs
Created August 13, 2013 18:00
An experimental Scheme parser that follows R6RS production rules.
module SchemeParser where
import Text.Parsec
import Text.Parsec.String
(<>) :: Parser [a] -> Parser [a] -> Parser [a]
p1 <> p2 = do r1 <- p1; r2 <- p2; return (r1 ++ r2)
one :: Parser a -> Parser [a]
one = fmap (:[])
@lylek
lylek / toy_scheme.hs
Created August 13, 2013 16:36
A simple interpreter for a subset of Scheme in Haskell. Meant to be used as a base for experiments.
import Control.Monad
import Data.Char
import Text.Parsec
import Text.Parsec.String
data SExpr =
SSym { getSym :: String } |
SInt !Integer |
SBool !Bool |
SList [SExpr] |