ocaml

behavior explanation for higher order functions and labeled argument in OCaml

孤者浪人 提交于 2019-12-24 09:15:20
问题 Taking an exemple derived from RWOCaml : utop # let divide ~first ~second = first / second;; val divide : first:int -> second:int -> int = <fun> utop # let apply_to_tuple_3 f (first,second) = f second first;; val apply_to_tuple_3 : ('a -> 'b -> 'c) -> 'b * 'a -> 'c = <fun> utop # apply_to_tuple_3 divide;; Error: This expression has type first:int -> second:int -> int but an expression was expected of type 'a -> 'b -> 'c Does it make sense to not match the types here ? apply_to_tuple_3 only

OCaml syntax trap: multiple lets using separators

夙愿已清 提交于 2019-12-24 08:20:07
问题 I am trying to teach myself OCaml. I've been going nuts over this one syntax trap. I'm told that you can string expressions together in sequence using ";" ie, expr1 ; expr2 executes the first expr, then the second, as expected. For some reason, I cannot get the interpreter to agree with the following input let x = 5 ; let y = 7;; Bizarrely if ONLY the first expr is a let, it works. So let x = 5 ; 7;; Passes, and evaluates to 7. Even worse, if I attempt to use parens to compose multiple

OCaml : as keyword in pattern matching behaving strangely

﹥>﹥吖頭↗ 提交于 2019-12-24 06:49:17
问题 Let's say I write this code : # type t = A of int * int let f = function A (i1, i2) -> print_int i1;; type t = A of int * int val f : t -> unit = <fun> Perfect, it works. Now, let's say I have this wonderful function : # let print_couple (i1, i2) = print_int i1; print_int i2;; val print_couple : int * int -> unit = <fun> So, as you expect, I'd like to write the following # let f = function A (_ as c) -> print_couple c;; Well, I can't Error: The constructor A expects 2 argument(s), but is

Make menhir add user-defined functions from .mly to .mli

感情迁移 提交于 2019-12-24 06:48:28
问题 Menhir allows to add arbitrary ocaml code to the end of the .mly file, where I want to declare a few functions. But I could not find a way to make menhir add my functions to the .mli file, so that they are visible from the other modules. Is it possible? 回答1: The answer is simple, it's no . The code defined in the .mly file is only used by the parser. As stated in the manual : A header is a piece of OCaml code, surrounded with %{ and %} . It is copied verbatim at the beginning of the .ml file.

OCaml: Lwt expression was expected of type unit Lwt.t

风格不统一 提交于 2019-12-24 05:55:00
问题 I wanted to write for my OUnit tests a little server that when a client connect on a socket, send data to the client and quit. Because I wanted to use this in my tests, I thought that I should create a server in a thread and create a client in a thread. So here is the code: open Sys open Unix open Lwt (* a simple server that send a message when a client connects * compile with * ocamlfind ocamlc -o lwt_server -package lwt,lwt.unix,unix -linkpkg -g lwt_server.ml * http://baturin.org/code/lwt

printing a float with runtime-selectable precision

谁说我不能喝 提交于 2019-12-24 05:18:42
问题 This is similar to this question but not exactly the same. i naively tried this: let s prec = "%." ^ (string_of_int prec) ^ "f" in Printf.printf (s 2) 1.23 but this is rejected, as well as replacing ^ by ^^ . is there any way to do this? 回答1: Since format string are type-safe they should be known at compile time. You can't take an arbitrary string and use it as a format string. This restriction still allows you to build formats from pieces, you just shouldn't forget to call format_of_string

Can I make OCaml produce stack traces on uncaught exceptions?

偶尔善良 提交于 2019-12-24 04:53:05
问题 In Java when an exception escapes the main() function, a stacktrace is printed to the console. Can you make OCaml programs do the same thing? 回答1: Yes, compile with -g and set OCAMLRUNPARM=b $ cat exc.ml let f () : int = raise End_of_file let g () = f () + 44 let _ = g() $ ocamlc -g -o exc exc.ml $ OCAMLRUNPARAM=b exc Fatal error: exception End_of_file Raised at file "exc.ml", line 2, characters 10-21 Called from file "exc.ml", line 5, characters 4-8 Called from file "exc.ml", line 7,

Use module Set Ocaml

陌路散爱 提交于 2019-12-24 04:27:37
问题 I am creating a program that uses a grammar and see if this grammar is LL (1). I want to use the module Set, but I have no idea how to proceed, of course, the type of the elements of the set will char, can you help? 回答1: This answer assumes that you already know how to determine if a grammar is LL (1), and are merely looking for help on the specific usage of the Objective Caml Set module. The standard library Set provides a functor that lets you construct your own set module, adapted for your

installing an Ocaml hump library on mutualized server

大城市里の小女人 提交于 2019-12-24 04:09:18
问题 I am trying to use the Ocaml csv library. I downloaded csv-1.2.3 and followed the installation instructions after installing findlib: Uncompress the source archive and go to the root of the package, Run 'ocaml setup.ml -configure', Run 'ocaml setup.ml -build', Run 'ocaml setup.ml -install' Now I have META , csv.a , csv.cma , csv.cmi , csv.cmx , csv.cmxa , csv.mli files in ~/opt/lib/ocaml/site-lib/csv repertory. The shell command ocamlfind list -describe gives csv A pure OCaml library to read

Sorting List with OCaml standard library function

北慕城南 提交于 2019-12-24 03:52:31
问题 I'm studying OCaml and and doing various exercises on ordering data. I would like to understand how to use the standard librari List for ordering For example I would like to sort this array using these functions [94; 50; 6; 7; 8; 8] List.sort List.stable_sort List.fast_sort List.unique_sort What is the syntax to do it ? 回答1: If you want to use these functions on your list, you have to specifiy the comparison function. Quote from the documentation: The comparison function must return 0 if its