Defined variables module, not fully working
This commit is contained in:
@ -40,6 +40,7 @@ let int_more_eq a b =
|
||||
let int_not a =
|
||||
if a > 0 then 0 else 1
|
||||
|
||||
(* converts an integer to a list of chars such that it is pretty and linear *)
|
||||
let rec fromIntToString (alphabet: string) (x: int) : string =
|
||||
let base = String.length alphabet in
|
||||
if x < 0 then
|
||||
@ -47,4 +48,108 @@ let rec fromIntToString (alphabet: string) (x: int) : string =
|
||||
else if x < base then
|
||||
String.get alphabet x |> String.make 1
|
||||
else
|
||||
(fromIntToString (alphabet) (x/base - 1)) ^ (String.get alphabet (x mod base) |> String.make 1)
|
||||
(fromIntToString (alphabet) (x/base - 1)) ^ (String.get alphabet (x mod base)
|
||||
|> String.make 1)
|
||||
|
||||
|
||||
let inclusion la lb =
|
||||
let rec aux la =
|
||||
function
|
||||
[] -> true
|
||||
| b::lb ->
|
||||
if List.mem b la
|
||||
then aux la lb
|
||||
else false
|
||||
in
|
||||
aux lb la
|
||||
|
||||
let subtraction la lb =
|
||||
let rec aux la =
|
||||
function
|
||||
[] -> la
|
||||
| b::lb ->
|
||||
aux (List.filter ((<>) b) la) lb
|
||||
in
|
||||
aux la lb
|
||||
|
||||
(* returns only the unique elements of l *)
|
||||
let unique l =
|
||||
let rec aux l acc =
|
||||
match l with
|
||||
| [] ->
|
||||
List.rev acc
|
||||
| h :: t ->
|
||||
if List.mem h acc
|
||||
then aux t acc
|
||||
else aux t (h :: acc)
|
||||
in
|
||||
aux l []
|
||||
|
||||
(* returns the unique elements of the concat of the lists *)
|
||||
let unique_union la lb =
|
||||
unique (la @ lb)
|
||||
|
||||
let unique_intersection la lb =
|
||||
let rec aux la lb acc =
|
||||
match la with
|
||||
[] -> acc
|
||||
| a::la ->
|
||||
if List.mem a lb
|
||||
then aux la lb (a::acc)
|
||||
else aux la lb acc
|
||||
in
|
||||
aux la lb [] |> unique
|
||||
|
||||
|
||||
(* Complicated way to drop the last element and add a new option element to the
|
||||
beginning *)
|
||||
let prev l a =
|
||||
match l with
|
||||
| [] ->
|
||||
[a]
|
||||
| _ ->
|
||||
a :: (List.map (fun x -> Some x) (l |> List.rev |> List.tl |> List.rev))
|
||||
|
||||
let pad l a n =
|
||||
let l = List.map (fun i -> Some i) l in
|
||||
if List.length l < n
|
||||
then
|
||||
l @ (List.init (n - List.length l) (fun _ -> a))
|
||||
else
|
||||
l
|
||||
|
||||
let pad_opt l a n =
|
||||
if List.length l < n
|
||||
then
|
||||
l @ (List.init (n - List.length l) (fun _ -> a))
|
||||
else
|
||||
l
|
||||
|
||||
let combine la lb =
|
||||
List.map2 (fun a b ->
|
||||
match b with
|
||||
None -> None
|
||||
| Some b -> Some (a, b)
|
||||
) la lb
|
||||
|
||||
let rec last_list l =
|
||||
match l with
|
||||
[] -> failwith "Utility.last_list, not enough items"
|
||||
| [a] -> a
|
||||
| _::ll -> last_list ll
|
||||
|
||||
let add_to_last_list (la: 'a list list) (a: 'a) : 'a list list =
|
||||
let rec aux la a =
|
||||
match la with
|
||||
[] -> [[a]]
|
||||
| [l] -> [a :: l]
|
||||
| l::la -> l :: (aux la a)
|
||||
in
|
||||
aux la a
|
||||
|
||||
let rec combine_thrice la lb lc =
|
||||
match (la, lb, lc) with
|
||||
| [], [], [] -> []
|
||||
| [a], [b], [c] -> [a, b, c]
|
||||
| a::la, b::lb, c::lc -> (a, b, c) :: (combine_thrice la lb lc)
|
||||
| _ -> []
|
||||
|
||||
Reference in New Issue
Block a user