ocaml

Understand Core's `Fn.const`

醉酒当歌 提交于 2019-12-03 13:18:11
Jane Street's Core lib has such a function: Fn.const . https://github.com/janestreet/core_kernel/blob/master/lib/fn.ml let const c = (); fun _ -> c val const : 'a -> 'b -> 'a produces a function that just returns its first argument I really don't understand it. What's the purpose of this function? In what scenario we have to use it? Why put (); first? Why not write it as let const c = fun () -> c ? this will give a function taking unit as parameter and always returns initial c . If I do let f = const 5 , f will become a function which takes '_a as parameter. What's the purpose of returning a

How to make a multi-level module hierarchy with (or without) Oasis

青春壹個敷衍的年華 提交于 2019-12-03 13:13:21
问题 Suppose I have a set of modules each of which being rather "bushy" with submodules. M1.X M2.X M3.X M1.Y M2.Y M3.Y M1.Z M2.Z M3.Z M1.W M2.W M3.W M1.Q M2.Q M3.Q M1.P M2.P M3.P Moreover, I'd like each of these bushes to sit under one master module. Home.M1 Home.M2 Home.M3 Now, it's easy to structure the project directory for each of M1 , M2 , and M3 using Oasis' Pack: option. In particular, what I like and am trying to solve for is (a) the ability to lay out my files in the standard .ml / .mli

Lazy “n choose k” in OCaml

試著忘記壹切 提交于 2019-12-03 12:59:54
问题 As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant. For example, choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]] Any ideas? I would like to have the whole thing to be lazy, outputting a lazy

“Error: unbound module” in OCaml

家住魔仙堡 提交于 2019-12-03 12:49:49
Here's a simple example of using the library Cohttp: open Lwt open Cohttp open Cohttp_lwt_unix let body = Client.get (Uri.of_string "http://www.reddit.com/") >>= fun (resp, body) -> let code = resp |> Response.status |> Code.code_of_status in Printf.printf "Response code: %d\n" code; Printf.printf "Headers: %s\n" (resp |> Response.headers |> Header.to_string); body |> Cohttp_lwt.Body.to_string >|= fun body -> Printf.printf "Body of length: %d\n" (String.length body); body let () = let body = Lwt_main.run body in print_endline ("Received body\n" ^ body) I'm trying to compile it ocaml my_test1

ocamlfind cannot see installed package

你。 提交于 2019-12-03 12:34:49
问题 I wanted to compile my project using command: ocamlfind ocamlopt -package ocamlnet -package batteries -package unix -linkpkg oauth.ml but I'm getting following error: ocamlfind: Package `ocamlnet' not found make: *** [oauth.cmi] Error 2 After some research on this problem I have read that there may be problem with packages installed via opam and packages installed before opam installation (in this case with ocamlfind) so I tried to check that and get stuck because ocamlfind is installed via

OCaml - What is an unsound type?

瘦欲@ 提交于 2019-12-03 12:07:44
Recently I was given the code List.fold_left (fun acc x -> raise x ; acc) 3 I'm completely fine with this partial application having a functional value of type exn list -> int , and the fact it yields a warning isn't surprising. I am, however, not certain what half of the warning means: Warning 21: this statement never returns (or has an unsound type.) I can't actually find any reference to this warning where it isn't the result of a non-returning statement. Even the man page for ocamlc only mentions non-returning statements for this warning, and warnings.ml refers to it merely as Nonreturning

Properly compiling modules in subfolders (ocamlbuild)

为君一笑 提交于 2019-12-03 12:07:38
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 a module into the project properly. I created a parser.mlpack file and filled it with the proper

Open and closed union types in Ocaml

自作多情 提交于 2019-12-03 11:56:28
问题 I'm looking into OCaml for the first time, having a bit of background with F# and Haskell. As such, a lot is familiar-looking, but one thing that isn't is the concept of "open" and "closed" unions (with the backtick and [< syntax). What are these useful for and how often are they used? 回答1: gasche's answer has good advice. I'm going to explain open and closed unions a bit more. First, you need to distinguish the two kinds of unions: basic variants (no backtick) and polymorphic variants (with

Would you please explain OCaml functors to me? [duplicate]

放肆的年华 提交于 2019-12-03 11:22:13
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: In Functional Programming, what is a functor? I don't know much about OCaml, I've studied F# for some time and quite understand it. They say that F# misses functor model, which is present in OCaml. I've tried to figure out what exactly functor is, but wikipedia and tutorials didn't help me much. Could you please illuminate that mystery for me? Thanks in advance :) EDIT: I've caught the point, thx to everyone who

OCaml |> operator

守給你的承諾、 提交于 2019-12-03 11:03:04
Could someone explain what the |> operator does? This code was taken from the reference here : let m = PairsMap.(empty |> add (0,1) "hello" |> add (1,0) "world") I can see what it does, but I wouldn't know how to apply the |> operator otherwise. For that matter, I have no idea what the Module.() syntax is doing either. An explanation on that would be nice too. Module.(e) is equivalent to let open Module in e . It is a shorthand syntax to introduce things in scope. The operator |> is defined in module Pervasives as let (|>) x f = f x . (In fact, it is defined as an external primitive, easier to