Skip to content

Instantly share code, notes, and snippets.

View cohalz's full-sized avatar
🌴
On vacation

cohalz cohalz

🌴
On vacation
View GitHub Profile
let pos n =
let rec neg(n, k,i) =
if(n-k < 0) then 0.0
else if i mod 2 = 0 then neg(n,k+1,i+1) +. 1.0 /. float_of_int(4* (n-k) + 1)
else neg(n,k,i+1) -. 1.0 /. (float_of_int(4*(n-k)+3)) in
neg(n,0,0)
let rec pow n x =
if n <= 1 then x
else let stack = pow (n/2) x in
if n mod 2 = 0 then stack*stack
else x*stack*stack
let cube x = pow 3 x
let rec pow2 x n =
if n <= 1 then x
else let stack = pow2 x (n/2) in
let integral f a b =
let n = 10000.0 in
let delta = (b -. a) /. n in
let rec approx f a b i sum =
if i > n then sum else (approx f) a b (i +. 1.0) (sum +. (((f(a +. (i -. 1.0) *. delta) +. f(a +. i *. delta)) *. delta) /. 2.0)) in
(approx f) a b 1.0 0.0
integral sin 0.0 3.14159265359
let f1 x1 x2 x3 = x1 + x2 + x3
let f2 f x1 = f (x1 + x1) + x1
let f3 f = f (1) (1) + 1
let uncurry f (x, y) = f x y
(*´・_・`*)
let rec repeat f n x =
if n > 0 then repeat f (n -1) (f x) else x
let fib n =
let (fibn, _) = repeat (fun (p, c) -> (c, c+p)) n (0, 1) in
fibn
let rec downto1 = function
| n -> if n < 1 then [] else n::downto1 (n-1)
let romanmap = [(1000,"M");(900,"CM");(500,"D");(400,"CD");(100,"C");(90,"XC");(50,"L");(40,"XL");(10,"X");(9,"IX");(5,"V");(4,"IV");(1,"I")]
let rec roman romanmap n =
match romanmap with
| [] -> ""
| x::xs -> if fst x <= n then snd x ^ roman romanmap (n - fst x) else roman xs n
let rec nested_length = function
| [] -> 0
| []::xs -> nested_length xs
| (x::ls)::xs -> 1 + nested_length (ls::xs)
let rec concat = function
| [] -> []
| []::xs -> concat xs
| (x::ls)::xs -> x :: concat (ls::xs)