ocaml

Basic Ocaml: How do I compile this?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 00:35:53
问题 Just beginning with ocaml and am struggling with the various compilers and tooling. E.g. ocamlopt , ocamlc , ocamlbuild , corebuild and so on. So, how do I compile the following? open Core.Std module Regex = Re2.Regex let ls pattern = let pat = Regex.create_exn pattern in let matcher = Regex.matches pat in Sys.ls_dir "." |> List.filter ~f:matcher |> List.iter ~f:(fun s -> print_string s; print_newline ()) let () = match In_channel.input_line stdin with | None -> print_string "No Input" | Some

On finding documentation

爷,独闯天下 提交于 2019-12-21 20:17:42
问题 So far, giving Google search strings such as "ocaml" regex "ocaml" hashtbl etc. has usually gotten me the documentation I've been looking for. One notable exception has been "ocaml" "String.Map" With this search, all I can find (documentation-wise, that is), is documentation for the String.map function of the String module (or is it "package"?). (If understand correctly, String.Map is a data structure, namely a map whose keys have type string , rather than a function.) Even more forceful

Is there a name for ADT with explicit subtyping?

最后都变了- 提交于 2019-12-21 20:07:54
问题 I'm looking for a proper name for a data type that combines ADT with explicit subtyping. In one of my applications, I use a structure similar to ADT to represent parse trees, on which I perform recursive pattern matching. I find it rather convenient if I could combine ADT with subtyping, as demonstrated in the example below: Note: the example is written in Haskell's syntax, but this is not Haskell code. data Empty = Empty data Expr = Int Int | Add Expr AddOp Expr data OptionalExpr = | Empty /

MonadFix in strict language

喜你入骨 提交于 2019-12-21 08:17:23
问题 I'm working on camlp4 extension for haskell-like do notation in Ocaml, and trying to figure out how GHC compiles recursive do-bindings (enabled with -XDoRec). I wonder if it possible for monadic fixpoint combinator to exist in strict language (like Ocaml/F#/SML/...)? If yes, how can it look like? Would it be very useful? 回答1: The F# computation expression syntax (related to Haskell do ) supports recursion: let rec ones = seq { yield 1 yield! ones } This is supported because the computation

Why does the OCaml std lib have so many non-tail-recursive functions?

北城以北 提交于 2019-12-21 07:32:38
问题 I have been rewriting many OCaml standard library functions to be tail-recursive lately. Given that this has entailed straight-forward CPS transformation, I am left puzzling over why the default versions are not written this way. As an example, in the standard library, map is defined as: let rec map f = function [] -> [] | a::l -> let r = f a in r :: map f l I have rewritten it to be: let map f l = let rec aux l k = match l with [] -> k [] | a::l -> aux l (fun rest -> k (f a :: rest)) in aux

What is the meaning of Warning 40: this record … contains fields that are not visible in the current scope

只谈情不闲聊 提交于 2019-12-21 07:14:03
问题 Please consider the following code: module A = struct type r = { i : int; s: string } end module B = struct type r = { i : int; s : string } end let f (x : A.r) : B.r = match x with { i; s } -> { i = 2*i; s = "" } Two modules define exactly the same record. A function f converts an A record to a B record. The warning is already emitted during compilation, but also visible interactively. On the ocaml cli, it seems that a call to f does the intended thing: # let x = f { i = 5; s = "ab" };;

“Eval” a string in OCaml

橙三吉。 提交于 2019-12-21 05:14:27
问题 I'm trying to "eval" a string representing an OCaml expression in OCaml. I'm looking to do something equivalent to Python's eval. So far I've not been able to find much. The Parsing module looks like it could be helpful, but I was not able to find a way to just eval a string. 回答1: Here is how to do it, but I didn't tell you. (Also the Parsing module is about Parsing, not executing code) #require "compiler-libs" (* Assuming you're using utop, if compiling then this is the package you need *)

Using a variant type constructor with just one tuple value

陌路散爱 提交于 2019-12-21 04:48:14
问题 # 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? 回答1: 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

“as” keyword in OCaml

六月ゝ 毕业季﹏ 提交于 2019-12-21 03:59:08
问题 In the answers for the tutorials for OCaml available at this site, some of the solutions, including the one for eliminating consecutive duplicates of list elements, is written as such: let rec compress = function | a :: (b :: _ as t) -> if a = b then compress t else a :: compress t | smaller -> smaller;; What is the relevance of the line a :: (b:: _ as t) ? Why can't I write it as a :: b :: t instead? 回答1: The t in b :: _ as t is bound to b :: _ . So the meaning is different. If you use the

Properly compiling modules in subfolders (ocamlbuild)

[亡魂溺海] 提交于 2019-12-21 03:54:06
问题 I recently decided to organize the files in my project directory. I moved the parsers I had for a few different file types into their own directory and also decided to use ocamlbuild (the as the project was getting more complicated and the simple shell script was not sufficient any longer). I was able to successfully include external projects by modifying myocamlbuild with some basic rules (calling ocaml_lib , I'll use ocamlfind some other time), but I am stuck on how to include the folder as