ocaml

How to run OCaml + Core script?

陌路散爱 提交于 2020-01-14 10:27:19
问题 I am trying the Real World OCaml book. It talks about installing OPAM, then OCaml and Jane Street Core. I got utop loaded properly per the book instructions so that it automatically loads Core. Without Core, I can run a generic OCaml script simply by ocaml script.ml without compiling. But this doesn't work if the script uses Core. What's the right command if I want to run a Core based OCaml script without compiling? 回答1: Running a script that depends on additional libraries requires you to

OCaml warning 31, compiler-libs, and ppx

纵然是瞬间 提交于 2020-01-13 19:47:31
问题 I'm porting my application from OCaml 4.02.3 to 4.03.0. Say you have the following in lexer.ml : type t = T [@@deriving sexp] let () = sexp_of_t |> ignore; print_endline "hai" I'm trying to run it as following: ocamlbuild -use-ocamlfind -pkg ppx_sexp_conv -cflags '-w @a-4-31' lexer.byte -- But I'm getting the following error: Warning 31: files lexer.cmo and /Users/vladimir/.opam/4.03.0+flambda/lib/ocaml/compiler-libs/ocamlcommon.cma(Lexer) both define a module named Lexer File "_none_", line

Is it possible to define an exception inside a function

柔情痞子 提交于 2020-01-13 04:59:05
问题 One way to implement "early returns" in OCaml is via exceptions: exception Exit let myfunc () = try for i = 0 to .... do if .. then raise Exit done; false with Exit -> true However, is there a way to declare this Exit exception in the body of the function, so its name is not visible to other functions in the module? (* I would like to do this, but it gives a syntax error *) let myfunc () = exception Exit try for i = 0 to .... do if .. then raise Exit done; false with Exit -> true 回答1: Yes,

About the pattern matching algorithm in OCaml

点点圈 提交于 2020-01-12 05:42:10
问题 I am writing a compiler for a functional language I designed with OCaml. I want my little language to have the feature of pattern matching, however, I got stuck in coming up with an algorithm to implement it. It seems really complicated as I dig into the problem. I can't find much useful information about the corresponding algorithm with google. I will be appreciated if someone can give me some hint or point me to the resources. Or are there any tricks to take advantage of OCaml's power in

Socket onread, onready, onclose event handler function in Ocaml

╄→гoц情女王★ 提交于 2020-01-11 11:02:13
问题 I am developing a protocol using TCP/IP socket in Ocaml and I am interested implementing the event driven approach. Basically, I want to make event handling functions that invokes whenever socket receives a new data or closed or opened. Is it possible to do in Ocaml without implementing it manually using multiple threads? Thanks, 回答1: Yes. Make a loop and use Unix.select to wait for events on your fds. You will have to set your sockets to non-blocking mode with Unix.set_nonblock so that your

Record type pattern matching in Ocaml

∥☆過路亽.° 提交于 2020-01-10 02:29:19
问题 I'm trying to use pattern matching to write a calculator application. Two major types defined as below: type key = Plus | Minus | Multi | Div | Equals | Digit of int;; type state = { lcd: int; (* last computation done *) lka: key; (* last key actived *) loa: key; (* last operation actived *) vpr: int (* value print on the screen *) };; let print_state s = match s with state (a,_,_,d) -> print_int a; //Here has the compile error print_newline(); print_int d; print_newline();; However, if I

How the sequence type is defined in ocaml

霸气de小男生 提交于 2020-01-06 05:52:32
问题 type 'a node = | Nil | Cons of 'a * 'a t and 'a t = unit -> 'a node type 'a mappable = 'a t What does 'a t = unit -> 'a node mean in a type declaration? I thought that in a type declaration in ocaml we can only do an enumeration or call a constructor. thank you 回答1: unit -> 'a node is the type of a function that takes no argument and returns a 'a node (a node parameterized by 'a ). Example : let f () = Nil;; type 'a t = unit -> 'a node makes a synonym of the above type which is used in the

OCaml count consecutive elements in a list

。_饼干妹妹 提交于 2020-01-06 04:30:29
问题 I'm writing OCaml code that reads in a list and removes any char 'i's that appear at the beginning of the list. For instance, the list removeI['i';'i';'a';'c';'i'] should return -: int * char list = ['a';'c';'i'] , because there are 2 'i's at the beginning of the list. I believe I know how to implement this properly; however, I want to return a tuple that includes the number of removed 'i's as well as the new list with the 'i's removed. I know that may sound confusing, but an example would be

OCaml count consecutive elements in a list

倖福魔咒の 提交于 2020-01-06 04:30:27
问题 I'm writing OCaml code that reads in a list and removes any char 'i's that appear at the beginning of the list. For instance, the list removeI['i';'i';'a';'c';'i'] should return -: int * char list = ['a';'c';'i'] , because there are 2 'i's at the beginning of the list. I believe I know how to implement this properly; however, I want to return a tuple that includes the number of removed 'i's as well as the new list with the 'i's removed. I know that may sound confusing, but an example would be

OCaml llvm “Unbound module ExecutionEngine”

我们两清 提交于 2020-01-06 04:00:46
问题 I made a post here (ocaml llvm kaleidoscope tutorial "Unbound module LlvmExecutionEngine") but that issue does not seem present on my mac which I have switched to. I'm trying to get this to work: https://github.com/llvm-mirror/llvm/tree/master/examples/OCaml-Kaleidoscope/Chapter7 from this tutorial http://llvm.org/docs/tutorial/OCamlLangImpl7.html (I'm 99% sure these two are by the same people) After getting around a few issues I have reached a stumbling block for the last few hours of dyn