]> matita.cs.unibo.it Git - helm.git/blobdiff - 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
diff --git a/helm/ocaml/mathql_interpreter/utility.ml b/helm/ocaml/mathql_interpreter/utility.ml
new file mode 100644 (file)
index 0000000..38856a0
--- /dev/null
@@ -0,0 +1,70 @@
+
+(*
+ * funzioni di utilita' generale
+ *)
+
+(*
+ * converte il risultato di una query in una lista di stringhe
+ *
+ * parametri:
+ * l: Postgres.result; risultato della query
+ *
+ * output: string list; lista di stringhe (una per tupla)
+ *
+ * assumo che il risultato della query sia
+ * costituito da un solo valore per tupla
+ *
+ * TODO
+ * verificare che l sia effettivamente costruita come richiesto
+ *)
+let pgresult_to_string_list l = List.map (List.hd) l#get_list;;
+
+(*
+ * converte il risultato di una query in una stringa
+ *
+ * paramteri:
+ * l: Postgres.result; risultato della query
+ *
+ * output: string; valore dell'unica tupla del risultato
+ *
+ * mi aspetto che il risultato contenga una sola tupla
+ * formata da un solo valore
+ *
+ * TODO
+ * verificare che l sia costruita come richiesto
+ *)
+let pgresult_to_string l = List.hd (List.hd l#get_list);;
+
+(*
+ * parametri:
+ * x: 'a; chiave di cui settare il valore
+ * v: 'b; valore da assegnare alla chiave
+ * l: ('a * 'b) list; lista di coppie in cui effettuare
+ *    l'assegnamento
+ *
+ * output: ('a * 'b) list; lista di coppie contenente (x, v)
+ *
+ * TODO
+ * gestire i casi in cui in l compaiono piu' coppie (x, _)
+ * si sostituiscono tutte? se ne sostituisce una e si eliminano
+ * le altre?
+ *)
+let set_assoc x v l =
+ let rec spila testa key value lista =
+  match lista with
+     []                      -> testa @ [(key, value)]
+  |  (j, _)::tl when j = key -> testa @ [(key, value)] @ tl
+  |  hd::tl                  -> spila (testa @ [hd]) key value tl
+ in
+  spila [] x v l
+;;
+
+(** TEST **)
+
+(*
+let h = ["d";"b"];;
+let v = ["1";"2"];;
+let c = List.combine h v;;
+
+List.iter (fun (a,b) -> print_endline (a ^ ": " ^ b)) (set_assoc "a" "3" c);;
+*)