Skip to content

Instantly share code, notes, and snippets.

View Spreadsheet.hs
{-# LANGUAGE GADTs #-}
module Spreadsheet where
import Control.Applicative
import Control.Monad (forM_)
import Data.IORef
import Data.List (union)
import Data.Unique
@chris-taylor
chris-taylor / design.hs
Created June 23, 2015 09:59
scrappy code to find (n,k,t) designs with greedy algorithm
View design.hs
select [] = []
select (x:xs) = (x,xs) : select xs
pairs nums = [(a, b) | (a, bs) <- select nums, b <- bs]
triples nums = [(a,b,c) | (a,bs) <- select nums, (b,cs) <- select bs, c <- cs]
overlap (a,b,c) (d,e,f) =
case compare a d of
@chris-taylor
chris-taylor / interpreter.hs
Last active August 29, 2015 14:04
Interpreter for Hanno's made-up assembly language
View interpreter.hs
import Data.Map (Map, (!), insert, empty)
import Control.Monad.State (StateT, put, get, liftIO, evalStateT)
type Val = Int
type Var = String
data Instruction =
Set Val Var
| Print Var
| Add Var Var Var
View FXForwards.html
<h1 id="fx-forwards">FX Forwards</h1>
<p>The standard no-arbitrage price <span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-1-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-1" style="width: 2.069em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.715em; height: 0px; font-size: 121%;"><span style="position: absolute; clip: rect(1.243em 1000.003em 2.6em -0.469em); top: -2.122em; left: 0.003em;"><span class="mrow" id="MathJax-Span-2"><span class="msubsup" id="MathJax-Span-3"><span style="display: inline-block; position: relative; width: 1.656em; height: 0px;"><span style="position: absolute; clip: rect(1.243em 1000.003em 2.305em -0.469em); top: -2.122em; left: 0.003em;"><span class="mi" id="MathJax-Span-4" style="font-family: MathJax_Math; font-style: italic;">F<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.121em;"></span></span><span style="display: inline-block; width: 0px; h
View FXForwards.md

FX Forwards

The standard no-arbitrage price $F_{t,T}$ for currency forwards when the spot price is $S_t$, the domestic rate is $r_d$ and the foreign rate is $r_f$ is

$$F_{t,T} = S_t (1 + (r_d-r_f)(T-t) )$$

and the price time $\delta t$ later is

View 99.ml
(* Define monadic operations for a list *)
let return x = [x]
let (>>=) lst f = List.concat (List.map f lst)
(* Generate the combinations of k distinct objects from a list *)
(* Simple version
@chris-taylor
chris-taylor / golfsort.hs
Last active January 4, 2016 19:39
Golf'd mergesort
View golfsort.hs
sortBy c=m.map(:[]) -- O(n log n)
where m[]=[];m[x]=x;m x=m(n x);n[]=[];n[x]=[x];n(x:y:z)=p x y:n z
p[]y=y;p x[]=x;p (w:x)(y:z)=case c w y of GT->y:p(w:x)z;_->w:p x(y:z)
sort x=m(map(:[])x)
where m[]=[];m[x]=x;m x=m(n x);n[]=[];n[x]=[x];n(x:y:z)=p x y:n z
p[]y=y;p x[]=x;p (w:x)(y:z)=if w>y then y:p(w:x)z else w:p x(y:z)
@chris-taylor
chris-taylor / wordgame.hs
Last active January 2, 2016 19:09
Given two words *from* and *to*, transform *from* into *to* by either adding, removing or altering one letter at a time, so that each step in the transformation is a valid English word.
View wordgame.hs
import Control.Applicative
import qualified Data.Set as Set
import AI.Search.Uninformed
step :: Set.Set String -> String -> [] String
step wordlist xs = Set.toList . Set.fromList . filter f $ remove1 xs ++ swap1 xs ++ add1 xs
where
f w = Set.member w wordlist
len = length xs
letters = ['a'..'z']
@chris-taylor
chris-taylor / dicegame.hs
Created January 9, 2014 23:49
You roll a dice up to three times. After each of the first two rolls you can choose to accept the number showing on the dice in dollars, or roll again. What is your expected winnings if you play optimally?
View dicegame.hs
import Control.Probability
expected p = expectation (runProb p)
die = uniform [1..6] :: Bayes Double Integer
game n
| n == 0 = die
| otherwise = do
x <- die
View ai-examples.hs
module AI.Search.Examples.Graph where
import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Data.STRef
import Data.Map (Map, (!))
import qualified Data.Map as Map
import Data.List (nub)
import Data.Graph.Inductive (LNode, LEdge, Gr)