ocaml

Is it possible to use OCaml in embedded mode?

五迷三道 提交于 2019-12-05 18:14:47
Some time ago I emerged for myself that Guile and Racket can be embedded and be called right from any C++ application. Can OCaml work like this? You can have a look at the Embedded O'Caml Toplevel done by Clément Capel. It's the result of a summer internship so it wasn't updated since 2004. Otherwise, there is ocamlmklib . You can use the OCaml toplevel as a library. It is part of the official OCaml distribution and up-to-date. See toploop.mli in OCaml sources for the interface. It is officially only available in bytecode, but there is experimental support for native code. If you plan to

Suggestion for solving fragile pattern matching

谁都会走 提交于 2019-12-05 18:04:27
I often need to match a tuple of values that should have the same constructor. The catchall _,_ always winds-up at the end. This of course is fragile, any additional constructor added to the type will compile perfectly fine. My current thoughts are to have matches that connect the first but not second argument. But, is there any other options? For example, type data = | States of int array | Chars of (char list) array let median a b = match a,b with | States xs, States ys -> assert( (Array.length xs) = (Array.length ys) ); States (Array.init (Array.length xs) (fun i -> xs.(i) lor ys.(i))) |

Why is this OCaml program faster than my C program?

帅比萌擦擦* 提交于 2019-12-05 15:51:04
问题 I wrote a basic Hippity Hop program in C, Python, and OCaml. Granted, this is probably not a very good benchmark of these three languages. But the results I got were something like this: Python: .350 seconds C: .050 seconds interpreted OCaml: .040 seconds compiled OCaml: .010 The python performance doesn't really surprise me, but I'm rather shocked at how fast the OCaml is (especially the interpreted version). For comparison, I'll post the C version and the OCaml version. C #include <stdio.h>

Ocaml utop library paths, Core module

五迷三道 提交于 2019-12-05 14:36:00
问题 I am attempting to use the Core module in utop , as originated by Jane Street and installed using opam . Here's the problem utop # open Core.Std;; Error: Unbound module Core utop does not seem to have the path to the Core module. How do you specify a path that can be found by utop to access the Core module? Is there a utop init file that specifies library paths ? I have the same error message from the OCaml 4.01.0 interpreter. The only way I can avoid this error is actually changing directory

reversing a list in OCaml using fold_left/right

匆匆过客 提交于 2019-12-05 13:02:51
UPDATE - Solution Thanks to jacobm for his help, I came up with a solution. // Folding Recursion let reverse_list_3 theList = List.fold_left (fun element recursive_call -> recursive_call::element) [] theList;; I'm learning about the different ways of recursion in OCaml (for class) and for some exercise, I'm writing a function to reverse a list using different recursion styles. // Forward Recursion let rec reverse_list_forward theList = match theList with [] -> [] | (head::tail) -> (reverse_list_1 tail) @ [head];; // Tail Recursion let rec reverse_list_tail theList result = match theList with [

Better to use “and” or “in” when chaining “let” statements?

≯℡__Kan透↙ 提交于 2019-12-05 12:44:41
问题 I realize this is probably a silly question, but... If I'm chaining a bunch of let statements which do not need to know each other's values, is it better to use and or in ? For example, which of these is preferable, if any: let a = "foo" and b = "bar" and c = "baz" in (* etc. *) or let a = "foo" in let b = "bar" in let c = "baz" in (* etc. *) My intuition tells me the former ought to be "better" (by a very petty definition of "better") because it creates the minimum number of scopes necessary

Tools for profiling OCaml code

霸气de小男生 提交于 2019-12-05 12:05:25
问题 Is anybody aware of programs for profiling OCaml code apart from using the -p option while compilation and then using gprof? I am asking this question in order to check if the sampling time of 0.01 second can be lowered further? 回答1: poorman's profiler is perfectly applicable for OCaml programs. The same idea works out for profiling allocations as well. 回答2: Never used it but ocamlviz is another option. 回答3: You can also use ocaml-memprof, a compiler patch (3.12.0 and 3.12 1) written by

OCaml literal negative number?

 ̄綄美尐妖づ 提交于 2019-12-05 12:00:58
I'm learning. This is something I found strange: let test_treeways x = match x with | _ when x < 0 -> -1 | _ when x > 0 -> 1 | _ -> 0;; If I then call it like this: test_threeways -10;; I will get type mismatch error (because, as far as I understand, it interprets unary minus as if it was partial function application, so it considers the type of the expression to be int -> int . However, this: test_threeways (-10);; acts as expected (though this actually calculates the value, as I could understand, it doesn't pass a constant "minus ten" to the function. So, how do you write constant negative

__memcpy_sse2_unaligned - what does this mean in detail?

烂漫一生 提交于 2019-12-05 11:13:21
问题 While working on my compiler I got this error: Program received signal SIGSEGV, Segmentation fault. __memcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:33 How do I get details of what went wrong here? I know from the backtrace it's a memcpy line that causes it, but how do I see how the memory is aligned? And how do I know how it should be aligned? The project is a compiler with an LLVM back-end using the Zend/PHP runtime with the OCaml garbage collector, so there

Inconsistent assumptions over interface (Ocaml)

丶灬走出姿态 提交于 2019-12-05 11:11:38
问题 I suddenly started to get this error. I don't know how to diagnose or fix it. Am I supposed to grep through bar.ml and check every Big_int function against signature in Big_int.mli ? File "foo.ml", line 1, characters 0-1: Error: The files /home/bar.cmi and /usr/lib/ocaml/big_int.cmi make inconsistent assumptions over interface Big_int 回答1: Generally this message means that one .mli file has been recompiled recently but not the other. Since one of the .mli files is in your OCaml library, it