Adding to a string map in OCaml -
i have been trying figure out figure pretty simple task, namely adding entries map of strings in ocaml within function. relevant elements below:
module stringmap = map.make (string);; let m = stringmap.empty;; let rec count ke = match ke |[] -> [] |hd::tl -> begin let m = stringmap.add hd 1 m; [hd] @ count tl end;;
i keep receiving cryptic "syntax error" message, have yet find solution after stripping code down nothing. can add string map single command, if try run let m = stringmap.add hd 1 m
within function, fails run. sure simple problem, can please help? thanks.
there seem few problems code. first, here example on how trying do:
module stringmap = map.make (string) let rec count m ke = match ke | [] -> m | hd :: tl -> count (stringmap.add hd 1 m) tl let m = count stringmap.empty ["foo"; "bar"; "baz"]
some comments on original code:
- double semicolons used tell repl loop want consume code. should avoid them if not using it.
- single semicolons used separate imperative expressions, e.g. following assignment. here have let expression, has syntax "let <..> in <..>", use of semicolon after wrong. ("let = ..." without "in" toplevel construct, , not can use locally).
- stringmap (and other structures in ocaml) functional, not imperative. can't mutate 'm' declared in first line. have build structure, , return built structure in end of loop.
Comments
Post a Comment