ocaml

Stack overflow using recursive functions in OCaml

Hi, I have a little problem: I want to solve this problem with OCaml, so I tried this -> -> let rec somme x = if ( nor (bool_of_int (x mod 3)) (bool_of_int (x mod 5))) then x + (somme x-1) else (somme x-1) ;; val somme : int -> int = <fun> -> somme 1000 ;; Stack overflow during evaluation (looping recursion?). What have I done wron...

OCaml empty global variable

Hi all! I'd like to know how do I define an empty! global variable of type Hashtbl in OCaml? I don't want to use Hashtbl.create because I don't know its initial size and I don't want to guess the initial size for performance reasons. Basically this Hashtbl variable will be assigjned a real Hashtbl in a function and then this variable w...

Higher-order type constructors and functors in Ocaml

Can the following polymorphic functions let id x = x;; let compose f g x = f (g x);; let rec fix f = f (fix f);; (*laziness aside*) be written for types/type constructors or modules/functors? I tried type 'x id = Id of 'x;; type 'f 'g 'x compose = Compose of ('f ('g 'x));; type 'f fix = Fix of ('f (Fix 'f));; for types but it d...

How to associate an iterator to a collection in OCaml

I have these two classes in OCaml class type ['a] collection = object method add : 'a -> unit method clear : unit -> unit method iterator : unit -> 'a iterator method remove : 'a -> unit end class type ['a] iterator = object method hasNext : unit -> bool method next : unit -> 'a end And I need to cr...

ocaml record type and null

I'm trying to define type like: type aaa = NULL | {a: int; b: int};; But the compiler does not allow to do it. I'm not sure of the reason why we can't mix record type with anything else. I need to match a value if it is the record type or null record and I'm tired of creating dummy record like {a = -999; b = -999}. is there a better ...

What is the standard OCaml data structure with fastest iteration?

I'm looking for a container that provides fastest unordered iterations through the encapsulated elements. In other words, "add once, iterate many times". Is there one among OCaml's standard modules that is fast enough (such that further optimization of it would be useless)? Or some kind of third-party GPL-ready ones? AFAIK there's ju...

Good projects to learn OCaml and F#

After learning the basic syntax, reading some non-trivial code is a fast way to learn a language. We can also learn how to design a library/software during reading others' code. I have following lists. A Chess program in OCaml by Tomek Czajka. Hal Daumé has written several machine learning libraries in Ocaml. Including decision tree...

Using "ocamlfind" to make the OCaml compiler and toplevel find (project specific) libraries

Hello, I'm trying to use ocamlfind with both the OCaml compiler and toplevel. From what I understood, I need to place the required libraries in the _tags file at the root of my project, so that the ocamlfind tool will take care of loading them - allowing me to open them in my modules like so : open Sdl open Sdlvideo open Str Currentl...

In Functional Programming, what is a functor?

I've come across the term 'Functor' a few times while reading various articles on functional programming, but the authors typically assume the reader already understands the term. Looking around on the web has provided either excessively technical descriptions (see the Wikipedia article) or incredibly vague descriptions (see the section ...

OCaml resources?

Hello, I need to learn this language for my thesis so I was wondering which good resources are available like books or free tutorials? Is there any "bible" reference book that I should consider? Ok, I can search over google but I think that having feedback about resources works better if it's directly from ocaml programmers.. ...

Properly compiling modules in subfolders (ocamlbuild)

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 p...

is it possible to reproduce python's string interpolation in ocaml?

In python, one can use printf like formatting with the "%" operator: "i am %d years old" % 99 or "%s is %d years old" % ("bob", 101) Is there a way to get the same concise syntax in Ocaml, for arbitrary numbers of arguments? For a single argument, the following works: let (%) = Printf.sprintf in ... "i am %d years old" % 99 Is ...

Collecting the output of an external command using OCaml

What is the right way to call an external command and collect its output in OCaml? In Python, I can do something like this: os.popen('cmd').read() How I can get all of an external program's output in OCaml? Or, better, OCaml with Lwt? Thanks. ...

Save image with ocaml graphics

Hello, i want to save the picture generated by ocaml graphics in a file (png or jpeg). Thank you. ...

Machine learning in OCaml or Haskell?

I'm hoping to use either Haskell or OCaml on a new project because R is too slow. I need to be able to use support vectory machines, ideally separating out each execution to run in parallel. I want to use a functional language and I have the feeling that these two are the best so far as performance and elegance are concerned (I like Cl...

Stack overflow in OCaml and F# but not in Haskell

I've been comparing for fun different languages for speed in execution of the following program: for i from 1 to 1000000 sum the product i*(sqrt i) One of my implementations (not the only one) is constructing a list [1..1000000] and then folding with a specific funtion. The program works fine and fast in Haskell (even when using foldl ...

Factoring product type assignments in OCaml

I'm generally unsatisfied with writing code like this: let load_record_field cursor gets geti gett a = function | 0x01 -> let c, s = gets () in (a.a_record_uuid <- s; `More_record c) | 0x02 -> let c, s = gets () in (a.a_group <- s; `More_record c) | 0x03 -> let c, s = gets () in (a.a_title <- s; `More_record c) | 0x04 -> let c, ...

Does this function use tail recursion?

I am wondering if oCaml optimizes this code to be tail recursive and if so does F#? let rec sum xs = match xs with | [] -> 0 | x :: xs' -> x + sum xs' ...

What does :: and ' mean in oCaml?

What doesx :: xs' mean? I dont have much functional experience but IIRC in F# 1 :: 2 :: 3 :: [];; creates an array of [1,2,3] so what does the ' do? let rec sum xs = match xs with | [] -> 0 | x :: xs' -> x + sum xs' ...

How to update records with fields, which support in-place modifications, in a functional way in OCaml?

Assume I have a record with a Hashtbl field: type rec = { table : (int, int) Hashtbl.t; value : int; (* more fields... *) } How should I update it in a functional way, i.e. something like that: let new_rec = { old_rec with value = old_rec.value + 1 ; (* that's ok *) table = hash_table + (key -> value binding...