ocaml

Ocaml list to tree

浪尽此生 提交于 2019-12-11 05:26:03
问题 I want to write a function load: 'a option list -> 'a tree , that restores binary tree from the given list, which contains the elements in postfix order. If the list does not represent any tree, your function should raise the exception Load (you have to declare it earlier). The tree is defined as : type ‘a btree = L of ‘a | N of ‘a btree * ‘a btree So far what I did was : exception Load ;; let rec load l = match l with | [] -> raise Load | Some x::_ -> L x | None::t -> N(?,load t) ;; Here's

How to represent algebraic data types and pattern matching in JavaScript

倾然丶 夕夏残阳落幕 提交于 2019-12-11 04:47:54
问题 In functional language like OCaml, we have pattern matching. For example, I want to log users' actions on my website. An action could be 1) visiting a web page, 2) deleting an item, 3) checking the profile of another user, etc. In OCaml, we can write something as follows: type Action = | VisitPage of string (* www.myweb.com/help *) | DeletePost of int (* an integer post id *) | ViewUser of string (* a username *) However, I am not sure how to define this Action in JavaScript. One way I could

Why is `;;` giving me a syntax error in utop?

让人想犯罪 __ 提交于 2019-12-11 04:26:07
问题 I'm working on a short project to convert small programs from python to java and visa versa. I created the following code, and tested it in utop. let c = let x = "for (int i = 0; i<10; i++)" and y = "for i in range(0,10):" in function | x -> y | y -> x | _ -> "Oh no!!";; For some reason, x & y are both considered unbound, yet at the same time any parameter seems to match to x. What order does everything need to be written in to make this work? 回答1: Simply to follow up with your answer. In

how to force OCaml to infer a more general type?

寵の児 提交于 2019-12-11 03:40:47
问题 I want to define a function that accepts an optional argument which is a function ('a -> 'b). The default value should be the identity, which is actually ('a -> 'a) , but i see no reason why it should not be compatible with the more general ('a -> 'b) . When i try: let optional_apply ?f i = match f with | None -> i + 4 | Some fn -> fn (i + 4) I always get the narrow type ?f:(int -> int) -> int -> int . But I want to keep f as int -> 'b . What can i do? Or is this just unsound, since optional

GODI-Batteries: Installation problems

巧了我就是萌 提交于 2019-12-11 03:25:40
问题 I am trying to install godi-batteries using GODI console. I seem to have all dependencies sorted (like Camomile). I get the following error within Godi's interface: > ocamlfind ocamlopt -shared -linkall -package camomile,num,str -o src/batteries_uni.cmxs src/batteries_uni.cmxa > + ocamlfind ocamlopt -shared -linkall -package camomile,num,str -o src/batteries_uni.cmxs src/batteries_uni.cmxa > ld: warning: -read_only_relocs cannot be used with x86_64 > ld: codegen problem, can't use rel32 to

Order of evaluation for short-circuit operators and let in OCaml

大城市里の小女人 提交于 2019-12-11 03:09:50
问题 In OCaml, when using a let to assign an alias to a short-circuit operator ( && or || ), it no longer short-circuits evaluation of the operands. This is not intuitive. What is the reason for this behavior? Consider the following code: let f() = Printf.printf "f"; false;; let g() = Printf.printf "g"; true;; let a = (&&);; f() && g();; (* outputs 'f' *) (&&) (f()) (g());; (* outputs 'f' *) a (f()) (g());; (* outputs 'gf' *) This also happens with let ... in , so let b = (&&) in b (f()) (g());;

How to trace “failwith” error in Emacs?

孤街浪徒 提交于 2019-12-11 03:01:44
问题 I am writing OCaml under Emacs. I have already configured Emacs so that Meta-x compile and make -k gives warnings with hyperlink. But for errors raised by failwith , it can not give a hyperlink, for instance: analyzing (ZONE)... Fatal error: exception Failure("to do") Raised at file "pervasives.ml", line 22, characters 22-33 Called from file "list.ml", line 69, characters 12-15 make: *** [all] Error 2 Compilation exited abnormally with code 2 at Fri Jan 27 18:44:10 I have many failwith "to do

ocaml hash from mysql

让人想犯罪 __ 提交于 2019-12-11 02:56:11
问题 I have a large database (approx. 150 000 records) that I want to embed into an OCaml source code named compute.ml. I am trying (without success) to transform these tables into hashtables and to embed these hastables into a function compute , so as to have the binary program run quicly without having to do queries to an external sql database. I have 2 questions: Is there a way to export for once a mysql table into an associative array (Hashtbl) that can be accessed by (or even embedded into)

Why `id id` is not a value in OCaml?

南楼画角 提交于 2019-12-11 02:55:14
问题 I am still trying to understand the value restriction in OCaml and I was reading through Wright's paper. And in it states (fun x -> x) (fun y -> y) is not a syntactic value while it is also stating lambda expression should be a value. I am a bit confused here, isn't id id in its essence also a lambda expression? What really counts as a syntactic value in OCaml? I also tried it in utop and found these: utop # let x = let x = (fun y -> y) (fun z -> z) in x ;; val x : '_a -> '_a = <fun> Here id

this pattern-matching is not exhaustive in OCaml

拟墨画扇 提交于 2019-12-11 02:41:43
问题 I am new in OCaml and I wrote some code to get the n element of a list let rec n_elem l n = match n with | 0 -> match l with | h::_ -> h | _ -> failwith "erorr with empty list" | _ -> match l with | h::t -> n_elem t (n-1) | _ -> failwith "erorr with empty list" ;; When I run it using ocaml interpreter, an warning generate as: Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: 1 Warning 11: this match case is unused. and when I run it with: