Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The function make_assoc : string list list -> (int*int*string) list should trans

ID: 3886809 • Letter: T

Question

The function make_assoc : string list list -> (int*int*string) list should translate a list of lists of strings into associative form: the list [[s11;...;s1k]; [s21;...;s2m];...[sN1;....;sNl]] should be translated to [(1,1,s11);...;(1,k,s1k);(2,1,s21);...;(2,m,s2m);...;(N,1,sN1)...;(N,l,sNl)]. Some concrete evaluations:

make_assoc [] and make_assoc [[]] should both evaluate to []

make_assoc [["a"]] should evaluate to [(1,1,"a")]

make_assoc [["a";"b"];["c"]] should evaluate to (a permutation of) [(2,1,"c"); (1,2,"b"); (1,1,"a")]

It should follow as below:

Need some help with this. Thank you.

(* translate a string list list into associative form, i.e. *) (* a list of (row, column, entry) triples *) let make_assoc rc_list = []

Explanation / Answer

val counter = ref 1;
fun add_index (l,c)=

let
fun add_index_helper (c,_ ,nil) = nil
| add_index_helper (c,i,h::tl) = (c,i,h) :: add_index_helper (c,i+1,tl)
in
add_index_helper (!c,1,l)
end;

fun make_assoc [[]]=[] |
make_assoc[]=[]|
make_assoc (y::ys) = add_index(y,counter) :: make_assoc(ys);

Call make_assoc[["a","b"],["c"]]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote