ocaml

The correct way to build a Binary Search Tree in OCaml

女生的网名这么多〃 提交于 2019-12-08 21:05:36
Ok, I have written a binary search tree in OCaml. type 'a bstree = |Node of 'a * 'a bstree * 'a bstree |Leaf let rec insert x = function |Leaf -> Node (x, Leaf, Leaf) |Node (y, left, right) as node -> if x < y then Node (y, insert x left, right) else if x > y then Node (y, left, insert x right) else node The above code was said to be good in The right way to use a data structure in OCaml However, I found a problem. This insert will only work when building a bst from a list in one go, such as let rec set_of_list = function [] > empty | x :: l > insert x (set_of_list l);; So if we build a bst

Does compare work for all types?

两盒软妹~` 提交于 2019-12-08 18:12:42
问题 Let's consider a type t and two variables x,y of type t . Will the call compare x y be valid for any type t ? I couldn't find any counterexample. 回答1: The polymorphic compare function works by recursively exploring the structure of values, providing an ad-hoc total ordering on OCaml values, used to define structural equality tested by the polymorphic = operator. It is, by design, not defined on functions and closures, as observed by @antron. The recursive nature of the definition implies that

checking whether mutable list has cycle in ocaml?

倾然丶 夕夏残阳落幕 提交于 2019-12-08 17:32:11
问题 I'm trying to write a function to test whether a mutable list in Ocaml contains a cycle or not (that is, has a reference to itself and repeats continuously. My list is defined as type 'a m_list = Nil | Cons of 'a * (('a m_list) ref) . So far, I have: let is_cyclic list = let rec check xs = match (!xs) with |Nil -> false |Cons(_,v) -> ((!v)==list)||check v in match list with |Nil -> false |Cons(_, x) -> check x ;; but it's not quite right and I'm unsure how to proceed from here...thanks for

user-defined printer in OCaml

大城市里の小女人 提交于 2019-12-08 17:12:11
问题 printf , fprintf , etc. : all accept the %a conversion. The manual says for %a : "user-defined printer. Takes two arguments and apply the first one to outchan (the current output channel) and to the second argument. The first argument must therefore have type out_channel -> 'b -> unit and the second 'b. The output produced by the function is therefore inserted in the output of fprintf at the current point." I can't understand what a user-defined printer is for, and how you would implement and

Why is my string of type `bytes`?

瘦欲@ 提交于 2019-12-08 15:13:25
问题 According to Real World OCaml , the type of "abc" should be string . But actually in my utop REPL, it's of type bytes . I've already opened Core.Std . Why is that? (The version of OCaml is 4.02.2 ; Core is 112.24.01 ; utop is 1.18 .) 回答1: You must enable safe string mode explicitly. Just start utop with: $ utop -safe-string Before the introduction of type bytes in OCaml 4.02, strings were mutable. Now, strings are intended to be immutable, and bytes is the type to be used for "mutable strings

Manipulate unmutable variables inside of loop in OCaml

孤街浪徒 提交于 2019-12-08 13:26:56
问题 I have the following code in OCaml.I have defined all necesar functions and tested them step by step the evalution should work good but I didn't succed to manipulate the variables inside of while.How can I make x,vn,v to change their value?I think I should rewrite the while like a rec loop but can't figure out exactly: Here is the rest of code: http://pastebin.com/Ash3xw6y Pseudocode: input : f formula output: yes if f valid else not begin: V =set of prop variables eliminate from f => and <=>

not able to install anything using opam

孤人 提交于 2019-12-08 13:07:38
I installed opam with homebrew on Mac OS X 10.10. But I can't install anything using opam. This is an example error output that get when I try to install ocamlfind: # opam-version 1.2.0 # os darwin # command ./configure -bindir /Users/roger/.opam/system/bin -sitelib /Users/roger/.opam/system/lib -mandir /Users/roger/.opam/system/man -config /Users/roger/.opam/system/lib/findlib.conf -no-topfind # path /Users/roger/.opam/system/build/ocamlfind.1.5.5 # compiler system (4.02.1) # exit-code 1 # env-file /Users/roger/.opam/system/build/ocamlfind.1.5.5/ocamlfind-91360-c61e7b.env # stdout-file /Users

OCaml-Wodi Part 2: Compiling using what was installed

只愿长相守 提交于 2019-12-08 12:13:13
问题 I am trying to compile a very small vignette to see how lablgtk2 works. (* file: base.ml *) let main () = let window = GWindow.window () in window#show (); GMain.Main.main () let _ = main () I have it properly installed via the wodi32 package manager and checked the pgk-lib folder to see that there are gMain.cmx/cmi and gWindow.cmx/cmi files (as well as the respective .ml and .mli files). However, when I run the following compile command: $ ocamlc -I +lablgtk2 -o base lablgtk.cma gtkInit.cmo

OCaml Syntax Error fixed by double semicolon

北慕城南 提交于 2019-12-08 11:28:19
问题 I'm sorry for asking such a basic question here, but I'm getting a syntax error when trying to compile the following code, let sum_of_squares_of_two_largest x y z = let a :: b :: _ = List.sort (fun x y -> -(compare x y)) [x; y; z] in a * a + b * b; let rec factorial n = if n = 0 then 1 else n * factorial (n - 1); let e_term n = 1.0 /. float_of_int (factorial n); let rec e_approximation n = if n = 0 then (e_term 0) else (e_term n) +. (e_approximation (n - 1)); let rec is_even x = if x = 0 then

ocaml batteries installation

主宰稳场 提交于 2019-12-08 08:33:03
问题 There seems to be conflicting information about batteries installation. I have tried several suggestions, but none have worked for me. I first tried ocamlfind batteries/ocaml but that gave this error: ocamlfind: Cannot find command: /username/godi/lib/ocaml/pkg-lib/batteries/ocaml I then tried copying the ocamlinit file from the batteries directory to .ocamlinit in my home directory. This gave this error: Cannot find file topfind. File ".ocamlinit", line 38, characters 0-20: Error: Unbound