I need help creating a \"make_assoc\" function in Ocaml. Shown below is the para
ID: 3888166 • Letter: I
Question
I need help creating a "make_assoc" function in Ocaml. Shown below is the parameters and examples of what the final product should be:
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")]
Explanation / Answer
Association list is easy and simple for usage though the performance of it is never ideal. Many programming problems need to deal with key-value pair. The most efficient method of representing this in Ocaml is by using an association list.
An association list may be defined as a pair of key and value.
Assoc is an important module in ocaml for representing associative lists.
Program solution in ocaml using make_assoc module:
module make_assoc: sig .. end
type ('x, 'y) n = ('x * 'y) list
val add : ('x, 'y) n->
?equal:('x -> 'x -> bool) -> 'x -> 'y -> ('x, 'y) n
val find : ('x, 'y) n -> ?equal:('x -> 'x -> bool) -> 'x -> 'y option
val find_exn : ('x, 'y) n -> ?equal:('x-> 'x -> bool) -> 'x -> 'y
val mem : ('x, 'y) n -> ?equal:('x -> 'x-> bool) -> 'x -> bool
val remove : ('x, 'y) n ->
?equal:('x -> 'x -> bool) -> 'x -> ('x, 'y) n
val map : ('x, 'y) n -> f:('y -> 'z) -> ('x, 'z) n
val inverse : ('x, 'y) n -> ('y, 'x) n
val t_of_sexp : (Sexplib.Sexp.n -> 'x) ->
(Sexplib.Sexp.n -> 'y) -> Sexplib.Sexp.n-> ('x, 'y) n
val sexp_of_n : ('x -> Sexplib.Sexp.n) ->
('y-> Sexplib.Sexp.n) -> ('x, 'y) n-> Sexplib.Sexp.n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.