ocaml

Would you please explain OCaml functors to me?

Hi! I don't know much about OCaml, I've studied F# for some time and quite understand it. They say that F# misses functor model, which is present in OCaml. I've tried to figure out what exactly functor is, but wikipedia and tutorials didn't help me much. Could you please illuminate that mystery for me? Thanks in advance :) EDIT: I...

ocamlbuild; building toplevel

Having successfully reorganized my project for ocamlbuild with subdirectories and using ocamlfind, I've found it difficult to build the top-level. I've constructed a .mltop file containing all the modules that would be included and added the packages to the _tags, but the build doesn't work. It cannot find the C functions that are compi...

How do I intersect two lists in OCaml?

When I have two lists in OCaml, for example e1 = [3; 4; 5; 6; 7] and e2 = [1; 3; 5; 7; 9] Is there an efficient way to get the intersection of those two lists? I.e.: [3; 5; 7] Because I don't like scanning every element in list e2 for every element in list e1, thus creating a big Oh of order n^2. ...

OCaml/F# pattern matching variable binding

I have a function of the form 'a -> ('a * int) list -> int let rec getValue identifier bindings = match bindings with | (identifier, value)::tail -> value | (_, _)::tail -> getValue identifier tail | [] -> -1 I can tell that identifier is not being bound the way I would like it to and is acting as a variable within the match...

Why doesn't this short OCaml snippet with Printf.printf work?

I'm a OCaml newbie. I'm playing around with "hello world" type snippets and came across this situation. Here's a session with the interpreter with some extra comments: # let average a b = (a +. b) /. 2.;; val average : float -> float -> float = <fun> # average 1. 4.;; - : float = 2.5 # string_of_float (average 1. 4.);; - : string = "...

Ocaml exception handling for opening input channel

Hi! As a beginner in Ocaml, I have this current working code: ... let ch_in = open_in input_file in try proc_lines ch_in with End_of_file -> close_in ch_in;; Now I would like to add error handling for non-existing input files, I wrote this: let ch_in = try Some (open_in input_file) with _ -> None in match ch_in with | Some x -> t...

Lablgtk2 Installation on Windows.

Hi there people, I am currently relearning Ocaml and am in the need of a good editor. There is a new editor from OcamlForge: OCamlEditor http://ocamleditor.forge.ocamlcore.org/. Prerequisite for installation is Lablgtk2. Installing Lablgtk2 on windows is not straight forward and there is good instruction here: http://wwwfun.kurims.ky...

Filtering directly and indirectly connected things from list

Hello, if you have a function "test a b" which returns true if a and b are connected directly and if you have a given unordered list of things, what would be an elegant and fast solution to filter all connected things from given list? Example: let test a b = let diff = a - b in diff == 0 ;; let lst = [4;1;7;3;8;9;2;0] ;; filter_conn...

OCaml beginner needs help: What is wrong?

Code: let rec get_val (x, n) = match x with [] -> -1 | if (n=0) then (h::_) -> h else (_::t) -> get_val(t, n-1) ;; Error message: Characters 55-57: | if (n=0) then (h::_) -> h ^^ Error: Syntax error ...

First class modules in OCaml 3.12: What kinds of things will they make easier (or possible)?

I've heard that "first class modules" are coming in OCaml 3.12. What advantages will they offer? What kids of things will be easier? What problem are they trying to solve? A simple example would suffice. ...

Syntax error beyond end of program

I am experimenting with writing a toy compiler in ocaml. Currently, I am trying to implement the offside rule for my lexer. However, I am having some trouble with the ocaml syntax (the compiler errors are extremely un-informative). The code below (33 lines of it) causes an error on line 34, beyond the end of the source code. I am uns...

Tail-recursive merge sort in OCaml

Hello world! I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code: let tailrec_merge_sort l = let split l = let rec _split source left right = match source with | [] -> (left, right) | head :: tail -> _split tail right (head :: left) in _spli...

Is it possible to have a Ocaml function that accepts only integer lists?

I'm writing a recursive function in Ocaml that's supposed to count the number of items in an integer list (Yes I know there's a List.length function but I'm trying to do it myself). However the Ocaml compiler/interpreter forces me to use alpha list all the time. So is it wrong to say that, when a function accepts a list as a parameter,...

Is there any free OCaml to C translator?

So I have nice OCaml code (50000 lines). I want to port it to C. So Is there any free OCaml to C translator? ...

Is it possible to translate everething written in OCaml to PHP?

Is it possible to translate everething written in OCaml to PHP? for example will it be hard to translate such lib as lib for Fast content-aware image resizing ...

How to port OCaml lib into lib Acsessable from C code?

I have a OCaml library with lots of classes I need some translator to make it from OCaml lib a C lib so to be able to use its methods. How to do such thing? How to port OCaml lib into lib Acsessable from C code? ...

can a program written in C be faster than one written in OCaml and translated to C?

So I have some cool Image Processing algorithm. I have written it in OCaml. It performs well. I now I can compile it as C code with such command ocamlc -output-obj -o foo.c foo.ml (I have a situation where I am not alowed to use OCaml compiler to bild my programm for my arcetecture, I can use only specialy modified gcc. so I will compil...

How to compile ocaml to native code

i'm really interested learning ocaml, it fast (they said it could be compiled to native code) and it's functional. So i tried to code something easy like enabling mysql event scheduler. #load "unix.cma";; #directory "+mysql";; #load "mysql.cma";; let db = Mysql.quick_connect ~user:"username" ~password:"userpassword" ~database:"dat...

How do I do automatic data serialization of data objects in Haskell

One of the huge benefits in languages that have some sort of reflection/introspecition is that objects can be automatically constructed from a variety of sources. For example in Java I can use the same objects for persisting to a db (with Hibernate) serializing to XML (with JAXB) or serializing to JSON (json-lib). You can do the same in...

Creating GUI desktop applications that call into either OCaml or Haskell -- Is it a fool's errand?

In both Haskell and OCaml, it's possible to call into the language from C programs. How feasible would it be to create Native applications for either Windows, Mac, or Linux which made extensive use of this technique? (I know that there are GUI libraries like wxHaskell, but suppose one wanted to just have a portion of your application l...