View PredatorPrey.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let g = 0.02 and k = 500 and t = 1500 | |
let evolve(r, f) = | |
let dtrf = 0.0001 * r * f in | |
Some((r, f), (r + (1.0 - r/k)*r*g - dtrf, dtrf + (1.0 - g)*f)) | |
let () = | |
Seq.unfold evolve (50, 10) | |
@ Seq.truncate 1500 | |
@ Array.ofSeq |
View KMP.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let search p = | |
let m = # p in | |
let next = Array.init m [_ -> 0] in | |
let rec init(i, j) = | |
if i >= m-1 then () else | |
if Array.get p i = Array.get p j then | |
let () = Array.Unsafe.set next (i+1) (j+1) in | |
init(i+1, j+1) | |
else | |
if j=0 then init(i+1, j) else init(i, Array.get next j) in |
View Base64.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type State = | |
| S0 | |
| S1 Number | |
| S2 Number | |
let charOfNumber n = | |
if n<26 then Char.ofNumber(n+65) | |
else if n<52 then Char.ofNumber(n-26+97) | |
else if n<62 then Char.ofNumber(n-52+48) | |
else if n=62 then '+' else '/' |
View SuffixArray.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let suffixArray str = | |
let g = Array.get in | |
let n = # str in | |
let sa = Array.init n id in | |
let pos = Array.copy str in | |
let tmp = Array.init n [_ -> 0] in | |
let sufCmp gap (i, j) = | |
compare (g pos i, g pos j) @ | |
[ Equal -> | |
if i+gap >= n || j+gap >= n then compare(j, i) else |
View MaxSumSubseq.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let maxSumSubseq a = | |
let maxm = Array.fold [a, b -> max a b] 0 a in | |
let rec loop sum i = | |
if i = # a then sum else | |
let ai = Array.get a i in | |
if ai > 0 then loop (sum + ai) (i+1) else loop sum (i+1) in | |
loop 0 0 |
View PythagorasTree.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let branches m = | |
{ { Vec2.rotate(π/2 - asin(4/5)); | |
Vec2.scale(4/5, 4/5); | |
Vec2.translate(0, -1) }; | |
{ Vec2.translate(1, -1); | |
Vec2.rotate(-π/2 + asin(3/5)); | |
Vec2.scale(3/5, 3/5); | |
Vec2.translate(1/3, 0) } } | |
@ Array.map [m2 → Array.concat{m; m2}] |
View InfinitePowerSeries.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let toHtml f = | |
let x = Html "𝑥" in | |
let pow = (* xⁿ *) | |
[ 0 → {Html "1"} | |
| 1 → {x} | |
| n → {x; Html.tag "sup" {"style", "font-size:70%"} (Html.ofNumber n)} ] in | |
let append(xs, x) = Array.Unsafe.append xs x in | |
let appends(xs, xss) = Array.fold append xs xss in | |
let minus = Html.of " - " in | |
let ellipses = Html " + …" in |
View NQueens.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let safe (x1, y1) (x2, y2) = | |
x1 <> x2 && y1 <> y2 && x2 - x1 <> y2 - y1 && x1 - y2 <> x2 - y1 | |
let rec next n s = | |
Stack.pop s @ | |
[ None -> None | |
| Some((qs, ps), s) -> | |
Stack.pop ps @ | |
[ None -> if Stack.length qs = n then Some(qs, s) else next n s | |
| Some(q, ps) -> |
View CorrelationCoefficient.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pearsonsCorrelationCoefficient xys = | |
let n = Array.length xys in | |
let ∑xy, ∑x, ∑y, ∑x2, ∑y2 = | |
Array.fold [(∑xy, ∑x, ∑y, ∑x2, ∑y2), (x, y) → | |
∑xy+x*y, ∑x+x, ∑y+y, ∑x2+x*x, ∑y2+y*y] | |
(0, 0, 0, 0, 0) xys in | |
(n*∑xy-∑x*∑y)/(√(n*∑x2-∑x*∑x)*√(n*∑y2-∑y*∑y)) |
View Primes.tq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let primes = | |
let a = {2} in | |
let grow() = | |
let p0 = Array.get a (Array.length a - 1) + 1 in | |
let b = Array.init p0 [_ → True] in | |
let () = | |
Array.fold [(), di → | |
let rec loop i = | |
if i >= Array.length b then () else | |
let () = Array.Unsafe.set b i False in |
NewerOlder