ocaml

OCaml functors :: counter-intuitive behaviour

大城市里の小女人 提交于 2019-11-30 22:39:45
问题 I am experimenting with the module language of OCaml (3.12.1), defining functors and signatures for modules and so on, mostly following the examples from Chapter 2 of the OCaml manual and I've stumbled, by accident, on a situation where apparently my mental model of how functors and module signatures work is flawed. I tried to narrow the situation I encountered to the shortest amount of code possible so don't ask what I am trying to accomplish, this is a totally contrived example to

Looking for OCaml IDE [closed]

我的未来我决定 提交于 2019-11-30 21:45:59
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I like F# but sometimes I need something light and cross-platform and without .NET for sure. I tried to use OCamL many times but seems like I just can't start it. Installed IDEA, added OCamL plugin -> Doesn't work Installed eclipse ODT plugin -> Can't launch even config OCamL compiler - too complicated Even had

Converting F# pipeline operators ( <|, >>, << ) to OCaml

馋奶兔 提交于 2019-11-30 19:11:09
I'm converting some F# code to OCaml and I see a lot of uses of this pipeline operator <| , for example: let printPeg expr = printfn "%s" <| pegToString expr The <| operator is apparently defined as just: # let ( <| ) a b = a b ;; val ( <| ) : ('a -> 'b) -> 'a -> 'b = <fun> I'm wondering why they bother to define and use this operator in F#, is it just so they can avoid putting in parens like this?: let printPeg expr = Printf.printf "%s" ( pegToString expr ) As far as I can tell, that would be the conversion of the F# code above to OCaml, correct? Also, how would I implement F#'s << and >>

How do I declare a map type in Reason ML?

一个人想着一个人 提交于 2019-11-30 18:29:05
One advantage of Reason ML over JavaScript is that it provides a Map type that uses structural equality rather than reference equality. However, I cannot find usage examples of this. For example, how would I declare a type scores that is a map of strings to integers? /* Something like this */ type scores = Map<string, int>; And how would I construct an instance? /* Something like this */ let myMap = scores(); let myMap2 = myMap.set('x', 100); The standard library Map is actually quite unique in the programming language world in that it is a module functor which you must use to construct a map

How does the OCaml type inferencing algorithm work?

别来无恙 提交于 2019-11-30 18:18:01
I'm currently learning OCaml, and I'm curious to HOW OCaml does its type inferencing. I know that it's done through a process called unification, and I tried reading about the algorithm in the published paper but the notation threw me off. Can anyone describe the step-by-step process for me? Actually, it can be argued that unification is an implementation detail of the algorithm. The type system is only a set of rules. The rules allow to check an existing typing derivation. The rules do not mention unification explicitly, although unification is a technique that naturally comes to mind when

Memoization in OCaml?

风格不统一 提交于 2019-11-30 15:22:50
It is possible to improve "raw" Fibonacci recursive procedure Fib[n_] := If[n < 2, n, Fib[n - 1] + Fib[n - 2]] with Fib[n_] := Fib[n] = If[n < 2, n, Fib[n - 1] + Fib[n - 2]] in Wolfram Mathematica. First version will suffer from exponential explosion while second one will not since Mathematica will see repeating function calls in expression and memoize (reuse) them. Is it possible to do the same in OCaml? How to improve let rec fib n = if n<2 then n else fib (n-1) + fib (n-2);; in the same manner? You pretty much do what the mathematica version does but manually: let rec fib = let cache =

Make OCaml function polymorphic for int lists and float lists

99封情书 提交于 2019-11-30 15:07:33
问题 Is there a way to create a polymorphic add function in OCaml that works equally well for ints and floats? So for example if I have a function like: partialsums [1; 2; 3; 4; 5] I should get [1; 3; 6; 10; 15] but this function won't work on [1.; 2.; 3.; 4.; 5.] because in OCaml ints and floats absolutely cannot be mixed. But what if I want my function to work equally well for int lists and float lists? Is there a general type of which int and float are sub-types? If so, what is it? I'm a little

How to memoize recursive functions?

一笑奈何 提交于 2019-11-30 13:48:53
Consider a recursive function, say the Euclid algorithm defined by: let rec gcd a b = let (q, r) = (a / b, a mod b) in if r = 0 then b else gcd b r (This is a simplified, very brittle definition.) How to memoize such a function? The classical approach of defining a high-order function memoize : ('a -> 'b) -> ('a -> 'b) adding memoization to the function is here useless, because it will only save time on the first call. I have found details on how to memoize such function in Lisp or Haskell: How do I memoize a recursive function in Lisp? Memoization with recursion These suggestions rely on the

Topological sort in OCaml

时光总嘲笑我的痴心妄想 提交于 2019-11-30 12:49:42
问题 I'm trying to write topological sorting in ocaml, but I'm a beginner (in OCaml & graphs algorithms) and I can't do this by myself. It's easier for me to think about topological sorting in, for example, C++ (and there is a lot examples of topological sorting in C++ on the Internet), but I want to learn something new. Moreover, I've found some examples of topological sorting written in OCaml, but I don't understand them, to be frankly. Let's say I have a list (int * int list) list , for example

Is there any free OCaml to C translator? [closed]

自古美人都是妖i 提交于 2019-11-30 12:01:57
So I have nice OCaml code (50000 lines). I want to port it to C. So Is there any free OCaml to C translator? This probably isn't what you want, but you can get the OCaml compiler to dump its runtime code in C: ocamlc -output-obj -o foo.c foo.ml What you get is basically a static dump of the bytecode. The result will look something like: #include <caml/mlvalues.h> CAMLextern void caml_startup_code( code_t code, asize_t code_size, char *data, asize_t data_size, char *section_table, asize_t section_table_size, char **argv); static int caml_code[] = { 0x00000054, 0x000003df, 0x00000029, 0x0000002a