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
1 answer

How should one compare floats in OCaml?

In OCaml, comparing Integer 0 with Integer 0 returns true; however, comparing Float 0. to Float 0. returns false: # 0 == 0;; - : bool = true # 0. == 0.;; - : bool = false How does one compare floats correctly?
UnSat
  • 1,347
  • 2
  • 14
  • 28
4
votes
1 answer

Will OCaml compiler deal with boolean operators to make recursion tail-recursive?

Let's have a look at the following function is_prime: let is_prime n = let rec div_check i = i * i > n || (n mod i <> 0 && div_check (i+1)) in n >= 2 && div_check 2 So if n mod i <> 0 is false, then it will stop. But if n mod i <> 0 is true,…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
4
votes
3 answers

Why can't I add type constraints when implementing a module type?

I was trying (just out of interest) to do this: module type CAT = sig type ('a, 'b) t val id : ('a, 'a) t val (@) : ('b, 'c) t -> ('a, 'b) t -> ('a, 'c) t end module Lst = struct type ('a, 'b) t = 'a list constraint 'a = 'b let id = [] …
Thomas Leonard
  • 7,068
  • 2
  • 36
  • 40
4
votes
1 answer

OCaml compile & load during run-time

I am trying to achieve something similar to eval() in OCaml. I have a string and I want to get an OCaml function out of it. Currently I am doing the following: I dump the string to new.ml and compile the file: Compile.implementation…
Saswat Padhi
  • 6,044
  • 4
  • 20
  • 25
4
votes
1 answer

OCaml: Currying without defined values

I have two functions f and g and I am trying to return f(g(x)) but I do not know the value of x and I am not really sure how to go about this. A more concrete example: if I have functions f = x + 1 and g = x * 2 and I am trying to return f(g(x)) I…
nicotine
  • 2,519
  • 3
  • 19
  • 15
4
votes
1 answer

Implementing a simple factorial function using continuation passing style

I'm trying to implement a function that returns a factorial in OCaml but I don't know if I'm actually using a continuation passing style: let fact n = let rec factorial n cont = match n with | 0 -> cont () | _ -> factorial (n-1) (fun () ->…
newphew92
  • 41
  • 3
4
votes
2 answers

Why "Reference to undefined global `Moduletest'" in OCaml?

I wrote let fact x = let result = ref 1 in for i = 1 to x do result := !result * i; Printf.printf "%d %d %d\n" x i !result; done; !result;; in a file named "Moduletest.ml", and val fact : int -> int in a file named "Moduletest.mli". But,…
Zhipeng YANG
  • 797
  • 9
  • 18
4
votes
4 answers

Is this OCaml signature possible?

Is an OCaml function of signature: 'a -> 'b possible? I think it might be possible, however, I would not know the underlying logic behind the answer, so having it explained would be great :) EDIT: It cannot recursively loop forever
pkiller162
  • 352
  • 1
  • 11
4
votes
1 answer

Parametricity in OCaml

I am a complete beginner in OCaml, but I keep seeing code similar to the following in introductory examples let sum = List.fold ~f:(+.) ~init:0.0 What bothers me in this snippet is the explicit use of List.fold. In most languages I know, the…
Andrea
  • 20,253
  • 23
  • 114
  • 183
4
votes
1 answer

Use of return in deferred computation in Ocaml Async

I know that this question may look very silly but whenever I search to get a relevant answer, I only get general questions about return values, so... I'm studying deferred computation through OCaml, and I get the very basic concept of it. But when I…
Dmitri
  • 45
  • 2
4
votes
1 answer

ocaml 4.01.0 → 4.02.1, binary size became larger

On Ubuntu 14.04, 32 bit: ➥ cat test.ml let () = print_endline "hello"; ➥ opam switch list | grep " C " 4.01.0 C 4.01.0 Official 4.01.0 release ➥ ocamlopt test.ml ➥ ls -l a.out -rwxrwxr-x 1 shorrty shorrty 158569 Oct. 30 13:29 a.out ➥ opam…
shorrty
  • 2,431
  • 2
  • 17
  • 9
4
votes
1 answer

What toplevel directives does utop support?

I can't find any documentation on what toplevel directives I can use in utop. All I could find is this list of directives supported by the default ocaml toplevel, but that list doesn't seem to be complete for utop and is missing things like #typeof,…
hugomg
  • 68,213
  • 24
  • 160
  • 246
4
votes
5 answers

Is there an idiomatic way to do implicit local state in OCaml?

I want to write some code that builds a thing using some local state. For example, consider the following code that uses local state to generate sequential integers: type state = int ref let uniqueId : (state -> int) = fun s -> incr s; !s let…
hugomg
  • 68,213
  • 24
  • 160
  • 246
4
votes
1 answer

Convenient way to start a custom OCaml toplevel in Emacs with tuareg

If I start a custom toplevel in Emacs/tuareg with the tuareg-run-caml function, I need to give the path to the toplevel and the various -I options it requires to find the CMI files. This typing is tedious, is there a more convenient way to start a…
user40989
  • 228
  • 1
  • 6
4
votes
1 answer

Converting a constructor name to string in OCaml

I have the following type definitions in my code: type tag = | Head | Title | Body | H1 | P;; type domtree = | Empty | Node of tag * string * domtree list;; I need to print the tags along with the strings. But I couldn't find any way to convert…
Ruwangi
  • 243
  • 1
  • 4
  • 16