Skip to content

Instantly share code, notes, and snippets.

@lylek
lylek / StreamFork.hs
Last active December 17, 2018 05:18
Rate-limiting version of Stream.hs
{-# LANGUAGE BangPatterns, CPP #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing -fwarn-unused-imports #-}
-- -Wall
-- This is a rate-limiting version of https://github.com/simonmar/parconc-examples/blob/master/Stream.hs,
-- as per the exercise on page 69 of Parallel and Concurrent Programming in Haskell.
-- A module for stream processing built on top of Control.Monad.Par
-- (In the future may want to look into the stream interface used by
@lylek
lylek / ch18-exercises.lean
Created November 28, 2018 20:36
Proofs of theorems from section 17.4 of Logic & Proof
-- Logic & Proof Chapter 18 Exercises
namespace hidden
open nat
-- From 17.4
theorem mul_add (m n k : nat) : m * (n + k) = (m * n) + (m * k) :=
nat.rec_on k
-- Logic & Proof Chapter 18 Exercises
namespace hidden
open nat
-- From 17.4
theorem mul_add (m n k : nat) : m * (n + k) = (m * n) + (m * k) :=
nat.rec_on k
@lylek
lylek / logic_and_proof_ch17_ex19.hs
Last active November 18, 2018 22:05
Logic and Proof Exercise 17.19 Haskell solution
-- Logic and Proof Exercise 17.19
-- Finds an increasing list of Fibonacci numbers that sum to the given natural number.
-- The algorithm is linear in the input number, and tail recursive.
main = mapM_ print $ map fibsSummingTo [0..100]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
fibsSummingTo :: Integer -> [Integer]
fibsSummingTo n = loop n ltFibs []
@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
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
-- 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,
@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
@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-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]