Skip to content

Instantly share code, notes, and snippets.

def basketball(unseenShots: Int) = {
def prob(outcomes: List[Boolean]) = 1.0 * outcomes.count(m => m) / outcomes.size
val outcomes = always(List(true, false)).markov(unseenShots+1)(outcomes => tf(prob(outcomes)).map(b => b::outcomes)).given(_.head)
val lastShot = for {
outcome <- outcomes
shot <- tf(prob(outcome))
} yield shot
lastShot.pr(a => a)
}
@jliszka
jliszka / gist:2880013
Created June 6, 2012 04:55
Multiple phantom types in a single type parameter
object test {
sealed trait Limited
sealed trait Unlimited
sealed trait Lim extends Limited with Unlimited
sealed trait Skipped
sealed trait Unskipped
sealed trait Sk extends Skipped with Unskipped
import Data.List
import Test.QuickCheck
import qualified Data.Map as Map
nats = [1..]
divides a b = b `mod` a == 0
sieve (n:t) m = case Map.lookup n m of
Nothing -> n : sieve t (Map.insert (n*n) [n] m)
Just ps -> sieve t (foldl reinsert (Map.delete n m) ps)
where
// Multinomial formula
def multi(ks: Vector[Int]): Double = {
(1 to ks.sum)
.map(_ / ks.size.toDouble)
.zip(ks.flatMap(1 to _))
.map({ case (n, d) => n / d }).product
}
// Expected number of cereal boxes you have to buy if you want to collect at least `m` of each of `n` surprise mystery toys
def e(n: Int, m: Int, M: Int = 20, v: Vector[Int] = Vector.empty): Double = {
abstract class Dual(val rank: Int) {
self =>
// Cell value accessor
protected def get(r: Int, c: Int): Double
// Memoizing cell value accessor
def apply(r: Int, c: Int): Double = memo.getOrElseUpdate(r - c, self.get(r, c))
// The memo table
<?hh // strict
use namespace \HH\Lib\{C, Regex, Str, Vec, Math};
/*
"(3 + 4) - sin(3.14 / 2) * 3 * 2"
<exp> ::=
<product> "+" <exp>
| <product> "-" <exp>
| <product>
N=4
def gcd(a, b):
while b != 0:
(a, b) = (b, a % b)
return a
class Q(object):
def __init__(self, p, q=1):