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 rec mem a s =
match (a,s) with
| (_,[]) -> false
| (b,x::xs) -> if b = x then true else mem b xs
let rec max_list = function
| [] -> 0
| x::xs -> let max = max_list xs in if x >= max then x else max
let rec take lst n =
match (lst,n) with
| ([],_) -> []
| (x::xs,m) -> if m <= 0 then [] else x::(take xs) (m-1)
let rec drop lst n =
match (lst,n) with
| ([],_) -> []
| (x::xs,m) -> if m > 1 then take xs (m-1) else xs
let rec filter p = function
| [] -> []
| x::xs -> if p x then x::filter p xs else filter p xs
(*´・_・`*)
let rec unzip = function
| [] -> ([],[])
| (x,y)::xs -> (x::fst (unzip xs),y::snd (unzip xs))
let rec zip lst1 lst2 =
match (lst1,lst2) with
| ([],_) | (_,[]) -> []
| ((x::xs),(y::ys)) -> (x,y)::zip xs ys
let rec concat = function
| [] -> []
| []::xs -> concat xs
| (x::ls)::xs -> x :: concat (ls::xs)
let rec nested_length = function
| [] -> 0
| []::xs -> nested_length xs
| (x::ls)::xs -> 1 + nested_length (ls::xs)
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 downto1 = function
| n -> if n < 1 then [] else n::downto1 (n-1)