Skip to content

Instantly share code, notes, and snippets.

@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] |
@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 (:[])
#!/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
-- Semigroups and monoids
-- On Maybe:
-- Nothing disappears, left wins
instance Monoid (Maybe a) where
mempty = Nothing
Nothing `mappend` y = y
x `mappend` _ = x
@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]
@lylek
lylek / pearls-ch7.hs
Created March 8, 2018 04:52
Pearls of Functional Programming Chap. 7 code
#!/usr/bin/env stack
-- stack --install-ghc runghc --package=containers --package=pretty-tree --package=criterion --package=QuickCheck
-- Pearls of Functional Algorithm Design, Chap. 7
-- Building a tree of minimum height
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
import Control.DeepSeq (NFData, deepseq)
import Criterion.Main
@lylek
lylek / pearls-ch8.hs
Last active March 19, 2018 00:38
Pearls of Functional Programming, Chap. 8 Code
#!/usr/bin/env stack
-- stack --install-ghc runghc --package=criterion --package=QuickCheck --package=deepseq
-- Pearls of Functional Algorithm Design, Chap. 8
-- Unravelling greedy algorithms
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
import Control.DeepSeq (deepseq)
import Criterion.Main
-- Exercise 1
section
variable A : Type
variable f : A → A
variable P : A → Prop
variable h : ∀ x, P x → P (f x)
-- Show the following:
example : ∀ y, P y → P (f (f y)) :=
assume y,
hub ws (x : xs)
= minimum [is ++ nub ((x : xs) \\ is) | is <- inits ws]
= minimum [is ++ nub ((x : xs) \\ is) | is <- inits (us ++ vs)]
= minimum [is ++ nub ((x : xs) \\ is) | is <- inits us ++ map (us ++) (inits+ vs)]
= minimum ([is ++ nub ((x : xs) \\ is) | is <- inits us] ++ [is ++ nub ((x : xs) \\ is) | is <- map (us ++) (inits+ vs)])
= minimum ([is ++ nub ((x : xs) \\ is) | is <- inits us] ++ [us ++ is ++ nub ((x : xs) \\ (us ++ is)) | is <- inits+ vs])
= min (minimum [is ++ nub ((x : xs) \\ is) | is <- inits us]) (minimum [us ++ is ++ nub ((x : xs) \\ (us ++ is)) | is <- inits+ vs])
= min A B
A, when x not in xs
@lylek
lylek / logic_and_proof_ch_14_exercises.lean
Created September 13, 2018 01:07
Exercise for Chapter 14 of Logic and Proof
-- Logic and Proof Chapter 14 Exercises
-- Exercise 1
section
parameters {A : Type} {R : A → A → Prop}
parameter (irreflR : irreflexive R)
parameter (transR : transitive R)
local infix < := R