This file contains hidden or 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 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 |
This file contains hidden or 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 uncurry f (x, y) = f x y |
This file contains hidden or 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 f1 x1 x2 x3 = x1 + x2 + x3 | |
let f2 f x1 = f (x1 + x1) + x1 | |
let f3 f = f (1) (1) + 1 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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) |
This file contains hidden or 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 max_ascii str = | |
let rec itermax(i,str,max,length) = | |
if i >= length then max | |
else if int_of_char(str.[i]) > max then itermax(i+1,str,int_of_char(str.[i]),length) | |
else itermax(i+1,str,max,length) in | |
itermax(0,str,0,String.length str) |
This file contains hidden or 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 fib n = | |
let rec iterfib(i,pre2,pre1,res,n) = | |
if i >= n then res else iterfib(i+1,pre1,res,res+pre1,n) in | |
iterfib(1,0,0,1,n) |
This file contains hidden or 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 rec comb(n,m) = | |
if m = 0 || n = m then 1 | |
else comb(n-1,m) + comb(n-1,m-1) |
This file contains hidden or 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 rec gcd (m,n) = | |
if m mod n = 0 then n else gcd(n,m mod n) |