ocaml

How to make library installed from OPAM available to OCaml?

与世无争的帅哥 提交于 2019-12-10 09:39:12
问题 I followed this tutorial on OCaml FFI and installed Ctypes through OPAM: opam install ctypes However, OCaml does not find the module: open Ctypes (* ... *) I receive the error: Unbound module Ctypes It looks like I need to let OCaml know where my Ctypes installation is? Do I need to update some path variable to let OCaml look for my libraries installed through OPAM? This is Ubuntu 15.04, OCaml 4.01.0, OPAM 1.2.0. 回答1: Installing something on your system doesn't make it automatically visible

Is it possible to use OCaml in embedded mode?

萝らか妹 提交于 2019-12-10 09:31:19
问题 Some time ago I emerged for myself that Guile and Racket can be embedded and be called right from any C++ application. Can OCaml work like this? 回答1: You can have a look at the Embedded O'Caml Toplevel done by Clément Capel. It's the result of a summer internship so it wasn't updated since 2004. Otherwise, there is ocamlmklib. 回答2: You can use the OCaml toplevel as a library. It is part of the official OCaml distribution and up-to-date. See toploop.mli in OCaml sources for the interface. It

Wildcard pattern overriding subtype constraint on polymorphic variant

强颜欢笑 提交于 2019-12-10 04:35:20
问题 Given these types type a = [ `A ] type b = [ a | `B | `C ] and this function let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | `C -> "C" applying a value of type a works without issue, as expected: let a: a = `A let _ = pp a However, if the function is modified to include a wildcard pattern let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | _ -> "?" even though everything else remains the same, it now yields the following error (on let _ = pp a ): This expression has type

fold_tree in OCaml

你。 提交于 2019-12-10 03:45:58
问题 As You may know, there are higher order functions in OCaml, such as fold_left, fold_right, filter etc. On my course in functional programming had been introduced function named fold_tree, which is something like fold_left/right, not on lists, but on (binary) trees. It looks like this: let rec fold_tree f a t = match t with Leaf -> a | Node (l, x, r) -> f x (fold_tree f a l) (fold_tree f a r);; Where tree is defined as: type 'a tree = Node of 'a tree * 'a * 'a tree | Leaf;; OK, here's my

Emulating try-with-finally in OCaml

北城余情 提交于 2019-12-10 03:39:57
问题 OCaml's try .. with does not offer a finally clause like Java. It would be useful, though, especially when dealing with side effects. For example, I like to open a file, pass the open file to a function, and close it. In case the function raises an exception I have to catch it in order to have a chance to close the file. This gets increasingly complicated when multiple files are opened and opening itself might fail as well. Is there an established programming pattern to deal with this? Below

Ocaml: Lazy Lists

不问归期 提交于 2019-12-10 03:26:21
问题 How can I make a lazy list representing a sequence of doubling numbers? Example: 1 2 4 8 16 32 回答1: Using streams: let f x = Stream.from (fun n -> Some (x * int_of_float (2.0 ** float_of_int n))) or let f x = let next = ref x in Stream.from (fun _ -> let y = !next in next := 2 * y ; Some y) Using a custom lazy_list type: type 'a lazy_list = | Nil | Cons of 'a * 'a lazy_list lazy_t let rec f x = lazy (Cons (x, f (2*x))) 回答2: The great blog enfranchised mind has a great article on this topic:

Functors in Ocaml

时光毁灭记忆、已成空白 提交于 2019-12-10 01:21:17
问题 I am having a bit of a problem with a functor (and it's resultant type). Below, I have a Set functor that uses an Ordered type. I actually used the set.ml that comes with ocaml for some guidance, but I seem to be doing everything ahhem right. I created an Ordered module with integers and applied it to the Set functor to get the last module on this code sample, IntSet. The next line fails, when I try to insert an integer. I get the following type error: Error: This expression has type int but

Statically “extend” a record-ish data type without indirection hassle

烈酒焚心 提交于 2019-12-10 01:00:55
问题 I am currently working with a three-level process for which I need some information to flow being accessed and updated. The information is also three-leveled, in such a way that a process at one level may need to access/update information at its level and at higher levels. type info_0 = { ... fields ... } type info_1 = { ... fields ... } type info_2 = { ... fields ... } fun0 will do some stuff with an info_0 , then pass it to fun1 along with an info_1 , then get back the resulting info_0 and

Hashtable of mutable variable in Ocaml

耗尽温柔 提交于 2019-12-09 18:14:53
问题 I need to use hashtable of mutable variable in Ocaml, but it doesn't work out. let link = Hashtbl.create 3;; let a = ref [1;2];; let b = ref [3;4];; Hashtbl.add link a b;; # Hashtbl.mem link a;; - : bool = true # a := 5::!a;; - : unit = () # Hashtbl.mem link a;; - : bool = false Is there any way to make it works? 回答1: Mutable variables that may happen to have the same content can still be distinguished because they are stored at different locations in memory. They can be compared with the

Reading HTML contents of a URL in OCaml

試著忘記壹切 提交于 2019-12-09 17:35:10
问题 I would like to write an OCaml function which takes a URL and returns a string made up of the contents of the HTML file at that location. Any ideas? Thanks a lot! Best, Surikator. 回答1: I've done both of those things using ocurl and nethtml ocurl to read the contents of the URL (tons of properties here; this is the minimum), let string_of_uri uri = try let connection = Curl.init () and write_buff = Buffer.create 1763 in Curl.set_writefunction connection (fun x -> Buffer.add_string write_buff x