ocaml

How to set the default-directory of compilation in Emacs?

孤者浪人 提交于 2019-12-03 22:43:24
I am coding OCaml under Emacs, I have one makefile in the working folder, and several sub-folders containing .ml files. If I launch M-x compile and make works fine on a buffer of makefile , but does not work on a buffer of a .ml file, it gives me an error: -*- mode: compilation; default-directory: "..." -*- Compilation started at Fri Jan 27 18:51:35 make -k make: *** No targets specified and no makefile found. Stop. Compilation exited abnormally with code 2 at Fri Jan 27 18:51:35 It is understandable because the default-directory is sub-folder which does not contain makefile . Does anyone know

OCaml: Set modules

偶尔善良 提交于 2019-12-03 22:37:31
I want to use OCaml to generates sets of data and make comparisons between them. I have seen the documentation for Module types like Set.OrderType , Set.Make , etc, but I can't figure out how to initialize a set or otherwise use them. Sets are defined using a functorial interface. For any given type, you have to create a Set module for that type using the Set.Make functor. An unfortunate oversight of the standard libraries is that they don't define Set instances for the built-in types. In most simple cases, it's sufficient to use Pervasives.compare . Here's a definition that works for int :

How to implement a binary heap using list in OCaml?

给你一囗甜甜゛ 提交于 2019-12-03 22:12:39
问题 I am implementing a binary heap using list in OCaml, just to sharpen my OCaml skills. I feel it very difficult using list and after struggling for 2 days, I have to come here for suggestions and hints. Here is my thought so far Obviously, I can't use the orignal array based algorithm to implement it using list. What I am trying to utilise is binary tree . I have keep the invariant that a node should be bigger than any node whose level is lower than its. I roughly figured out how to implement

Polymorphism in OCaml - ad hoc,parametric, inclusion/subtyping

瘦欲@ 提交于 2019-12-03 16:21:53
I am having a problem understanding the different types of polymorphism, specifically in regards to OCaml. I understand that polymorphism allows for multiple types in OCaml denoted as 'a, but I do not understand what the different types of polymorphism are. If someone could give me an explanation with relatively low-level language that would be awesome! ad hoc, parametric, inclusion/subtyping Here's an approximation. Ad-hoc polymorphism usually refers to being able to declare the same name (usually a function) with different types, e.g. + : int -> int -> int and + : float -> float -> float in

Scripted main in OCaml?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 16:21:00
How can I emulate this Python idiom in OCaml? if __name__=="__main__": main() See RosettaCode for examples in other programming languages. There is no notion of main module in Ocaml. All the modules in a program are equal. So you can't directly translate this Python idiom. The usual way in Ocaml is to have a separate file containing the call to main , as well as other stuff like command line parsing that only make sense in a standalone executable. Don't include that source file when linking your code as a library. There is a way to get at the name of the module, but it's rather hackish, as it

What is the OCaml idiom equivalent to Python's range function?

天涯浪子 提交于 2019-12-03 14:42:55
问题 I want to create a list of integers from 1 to n. I can do this in Python using range(1, n+1), and in Haskell using: take n (iterate (1+) 1). What is the right OCaml idiom for this? 回答1: There is no idiom that I know of, but here is a fairly natural definition using an infix operator: # let (--) i j = let rec aux n acc = if n < i then acc else aux (n-1) (n :: acc) in aux j [] ;; val ( -- ) : int -> int -> int list = <fun> # 1--2;; - : int list = [1; 2] # 1--5;; - : int list = [1; 2; 3; 4; 5] #

Saving my running toplevel for later

↘锁芯ラ 提交于 2019-12-03 14:37:31
问题 When working in the ocaml or ghci toplevels I often build up a significant "context" for want of a better word, values bound, functions, modules loaded, and so on. Is there a way to save all of that and reload it later so I can continue exactly where I left off? Or better yet, dump out the entire lot as a text file that could be reloaded or be trivially modified into code that I could compile into an executable (e.g. by adding a Main)? 回答1: Users of HOL light have had similar needs, and they

Tail-recursive merge sort in OCaml

半世苍凉 提交于 2019-12-03 14:14:42
I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code: let tailrec_merge_sort l = let split l = let rec _split source left right = match source with | [] -> (left, right) | head :: tail -> _split tail right (head :: left) in _split l [] [] in let merge l1 l2 = let rec _merge l1 l2 result = match l1, l2 with | [], [] -> result | [], h :: t | h :: t, [] -> _merge [] t (h :: result) | h1 :: t1, h2 :: t2 -> if h1 < h2 then _merge t1 l2 (h1 :: result) else _merge l1 t2 (h2 :: result) in List.rev (_merge l1 l2 []) in let rec sort = function

Performance difference between pattern matching and if-else

99封情书 提交于 2019-12-03 13:58:57
Why can OCaml generate efficient machine code for pattern matching and not for if-else tests? I was reading Real World OCaml and I came across this section where they compared the performance of pattern matching to the performance of if-else tests. It turned out that pattern matching in their example was significantly faster than if-else tests. Even though the code doesn't utilize any special pattern match cases that would not be possible with if-else tests, it just compares integers. They ascribe compiler optimization for pattern matching as the reason for the performance difference. The

Using a variant type constructor with just one tuple value

ⅰ亾dé卋堺 提交于 2019-12-03 13:54:36
# type foo = Foo of int * int # let t = (1, 2) # Foo t Error: The constructor Foo expects 2 argument(s), but is applied here to 1 argument(s) How is it that I must do Foo (1, 2) to avoid that error even t has the appropriate type? This is one of the troubling parts of OCaml syntax, in my opinion. Despite the way it looks, the constructor Foo doesn't require a 2-tuple as its argument. It requires, syntactically, two values in parentheses--but they aren't a tuple. So it's simply the case that t has the wrong type. The way to make this work is to say: let (a, b) = t in Foo (a, b) The problem