]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/utility.ml
First very-very-very-very-alfa release of a MathQL Interpreter implemented
[helm.git] / helm / ocaml / mathql_interpreter / utility.ml
1
2 (*
3  * funzioni di utilita' generale
4  *)
5
6 (*
7  * converte il risultato di una query in una lista di stringhe
8  *
9  * parametri:
10  * l: Postgres.result; risultato della query
11  *
12  * output: string list; lista di stringhe (una per tupla)
13  *
14  * assumo che il risultato della query sia
15  * costituito da un solo valore per tupla
16  *
17  * TODO
18  * verificare che l sia effettivamente costruita come richiesto
19  *)
20 let pgresult_to_string_list l = List.map (List.hd) l#get_list;;
21
22 (*
23  * converte il risultato di una query in una stringa
24  *
25  * paramteri:
26  * l: Postgres.result; risultato della query
27  *
28  * output: string; valore dell'unica tupla del risultato
29  *
30  * mi aspetto che il risultato contenga una sola tupla
31  * formata da un solo valore
32  *
33  * TODO
34  * verificare che l sia costruita come richiesto
35  *)
36 let pgresult_to_string l = List.hd (List.hd l#get_list);;
37
38 (*
39  * parametri:
40  * x: 'a; chiave di cui settare il valore
41  * v: 'b; valore da assegnare alla chiave
42  * l: ('a * 'b) list; lista di coppie in cui effettuare
43  *    l'assegnamento
44  *
45  * output: ('a * 'b) list; lista di coppie contenente (x, v)
46  *
47  * TODO
48  * gestire i casi in cui in l compaiono piu' coppie (x, _)
49  * si sostituiscono tutte? se ne sostituisce una e si eliminano
50  * le altre?
51  *)
52 let set_assoc x v l =
53  let rec spila testa key value lista =
54   match lista with
55      []                      -> testa @ [(key, value)]
56   |  (j, _)::tl when j = key -> testa @ [(key, value)] @ tl
57   |  hd::tl                  -> spila (testa @ [hd]) key value tl
58  in
59   spila [] x v l
60 ;;
61
62 (** TEST **)
63
64 (*
65 let h = ["d";"b"];;
66 let v = ["1";"2"];;
67 let c = List.combine h v;;
68
69 List.iter (fun (a,b) -> print_endline (a ^ ": " ^ b)) (set_assoc "a" "3" c);;
70 *)