Fixes for RISC evaluation

This commit is contained in:
elvis
2025-01-11 20:32:11 +01:00
parent 3be05222ab
commit 9bcc88e016
11 changed files with 261 additions and 115 deletions

View File

@ -41,15 +41,15 @@ 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
""
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)
(* let rec fromIntToString (alphabet: string) (x: int) : string = *)
(* let base = String.length alphabet in *)
(* if x < 0 then *)
(* "" *)
(* 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) *)
(* true if every element of la is in lb *)
@ -93,7 +93,7 @@ let unique l =
(* returns the unique elements of the concat of the lists *)
let unique_union la lb =
unique (la @ lb)
la @ lb |> unique
(* returns all elements both in la and in lb *)
let unique_intersection la lb =
@ -107,6 +107,24 @@ let unique_intersection la lb =
in
aux la [] |> unique
(* given two lists of associations combines them and if an item is the same,
a provided function is applied to the associated values to create the new
association *)
let unique_union_assoc f l1 l2 =
let rec aux l acc =
match l with
| [] ->
acc
| (h1, h2) :: t ->
( match List.find_opt (fun (a, _) -> a = h1) acc with
| None -> aux t ((h1, h2) :: acc)
| Some (_h1, h3) -> aux
t
((h1, f h1 h2 h3) :: (List.remove_assoc h1 acc)) )
in
aux l2 (aux l1 [])
(* returns a list with at most n items and the rest in the second *)
let rec take (n: int) (l: 'a list) : ('a list * 'a list) =
match n with
@ -118,73 +136,27 @@ let rec take (n: int) (l: 'a list) : ('a list * 'a list) =
let (t1, t2) = (take (n - 1) ls) in
((i :: t1), (t2))
(* returns the list without the last element *)
let drop_last_element_list =
function
| [] -> []
| l -> l |> List.rev |> List.tl |> List.rev
(* takes a list and returns the same list without the first element;
different from List.tl since returns the empty list if there are not enough
items*)
let drop_first_element_list =
function
| [] -> []
| _::l -> l
(* 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) (drop_last_element_list l))
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
(* retuns the last element of a list *)
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
(* combines two lists into a list of tuples; different from List.combine since
lengths do not need to be equal, the functions return a list with length
equal to the minimum of the input lists *)
let rec combine_twice la lb =
match (la, lb) with
| [], [] -> []
| [a], [b] -> [a, b]
| a::la, b::lb -> (a, b) :: (combine_twice la lb)
| _ -> []
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)
| _ -> []