Questions tagged [ocaml]

OCaml is a strict statically-typed functional programming language, focusing on expressiveness, correctness, and efficiency.

#OCaml

OCaml is a strict statically-typed functional programming language, focusing on expressivity, correctness, and efficiency. These qualities make it the language of choice for complex software and timely go-to-market strategies.

For more information visit, the official OCaml site.

##Resources for OCaml Developers

##Resources for learning OCaml

Stack Overflow OCaml FAQ

  1. Documentation
  1. Editor
  1. The core language
  1. Loops/recursion
  1. Tools
  1. Good practices

#See also:#

7516 questions
4
votes
2 answers

Disable or enable code by preprocessor

In C++ I'd write bool positive (int a) { #ifdef DEBUG cout << "Checking the number " << a << "\n"; #endif return a > 0; } In OCaml I could write let positive x = begin printf "Checking the number %d\n" x; x > 0 end…
marmistrz
  • 5,974
  • 10
  • 42
  • 94
4
votes
2 answers

OCaml union types

I'm trying to make a function that uses a subtype of a defined type but OCaml wont derive the correct value : Consider this type definition: type fraction = {numerator : int; denominator : int};; type number = | Int of int | Fraction of…
yair
  • 95
  • 2
  • 9
4
votes
1 answer

The correct way to write unit tests for a module in OCaml

I have a given interface specification in the module.mli file. I have to write its implementation in the module.ml file. module.mli provides an abstract type type abstract_type I'm using OUnit to create the tests. I need to use the type's…
marmistrz
  • 5,974
  • 10
  • 42
  • 94
4
votes
3 answers

How do I split an OCaml list into head and tail?

I'm just learning OCaml and right now, I use match l with h::t -> to split lists when I need to access the head. But I'm sure there has to be a better way to split a list this way. I can't seem to find much online. Maybe I'm not looking in the right…
Antrikshy
  • 2,918
  • 4
  • 31
  • 65
4
votes
3 answers

How to list current package versions in OPAM?

opam list -a lists all packages currently available at OPAM, but does not display the version number for packages which are not currently installed, as per the opam list --help output: (...) the output format displays one package per line, and each…
anol
  • 8,264
  • 3
  • 34
  • 78
4
votes
3 answers

How to use GADTs across modules in OCaml without raising warnings?

I have two files: gadt1.ml and gadt2.ml and the second depends on the first. gadt1.ml: type never type _ t1 = A1 : never t1 | B1 : bool t1 type _ t2 = A2 : string t2 | B2 : bool t2 let get1 : bool t1 -> bool = function B1 -> true let get2 : bool t2…
Abdallah
  • 335
  • 1
  • 7
4
votes
2 answers

how to round a number in ocaml?

I want if it is bigger or equal to 0 to round it to the bigger number and if it is smaller than 0 to round it to the number before. Eg: If the number is 2.5 show 3 and if the number is -2.5 show -3. How should i write this? I wrote : let round x =…
Jeremy
  • 45
  • 1
  • 3
4
votes
1 answer

Check if a tree is a BST using a provided higher order function in OCAML

So let me start by saying this was part of a past homework I couldn't solve but as I am preparing for a test I would like to know how to do this. I have these implementations of map_tree and fold_tree provided by the instructor: let rec map_tree…
4
votes
1 answer

Can I realloc an OCaml GC block?

There is no realloc in OCaml memory.h or alloc.h (byterun/caml). Does that mean that is not possible to realloc an OCaml GC block (or value)? The use-case I'm thinking of is string concat, where the following can be optimized using realloc: a = a ^…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
4
votes
2 answers

How do I read this OCaml type signature?

I'm currently experimenting with using OCaml and GTK together (using the lablgtk bindings). However, the documentation isn't the best, and while I can work out how to use most of the features, I'm stuck with changing notebook pages (switching to a…
a_m0d
  • 12,034
  • 15
  • 57
  • 79
4
votes
1 answer

Ocaml: "contains type variables that cannot be generalized"

type ide = string;; type exp = http://pastebin.com/EhD9QdYj;; let emptyEnv = fun x -> raise EmptyEnvException;; let emptyFunEnv = fun x -> raise EmptyEnvException;; let raddoppia = Function("mul2", "x", Mul(Ide "x", Int 2));; (**)let funenv0 =…
alessandro308
  • 1,912
  • 2
  • 15
  • 27
4
votes
1 answer

OCaml sub-module constrained by a sub-signature

I have a module Mod that is constrained by signature Sig. The module has a Nested sub-module. The signature has a matching Nested sub-signature: module type Sig = sig val a : int module type Nested = sig val b : int end end module Mod :…
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
4
votes
1 answer

ocsigenserver.opt fails on startup with Unix.ECONNREFUSED

My ocsigen server has been working fine for a while, but just as I'm about to show it off to someone, it spews this on startup and then exits with failure: ocsigenserver.opt: ocsipersist:dbm: Cannot connect to Ocsidbm. Will continue without…
unhammer
  • 4,306
  • 2
  • 39
  • 52
4
votes
1 answer

Is it possible to open or use a functor without an intermediate module in OCaml?

Is it possible to open or use a functor without an intermediate module? For example, say we have the following set of modules and functors: module type FOO = sig val foo : int -> int end module Foo1 : FOO = struct let foo x = x+1 end module…
wyer33
  • 6,060
  • 4
  • 23
  • 53
4
votes
1 answer

OCaml: Converting strings to ('a, unit, string) format?

I need to convert from a string to a format. I'm given an array of formats, and I would like access each format and use sprintf on them. e.g. let errors = [| "format 1"; "format 2"; ... ; "format 512" |] let code_to_string (error_code : int)…
laifs
  • 197
  • 1
  • 2
  • 11