7,662 questions
4
votes
2
answers
689
views
Detect window closing OCaml Graphics
I haven't found details about window closing detection in the Graphics documentation. How detect it so I can trigger an action on closing ?
Thank you.
124
votes
5
answers
34k
views
Why is an int in OCaml only 31 bits?
Haven't seen this "feature" anywhere else. I know that the 32nd bit is used for garbage collection. But why is it that way only for ints and not for the other basic types?
35
votes
17
answers
24k
views
What is the OCaml idiom equivalent to Python's range function?
I want to create a list of integers from 1 to n. I can do this in Python using range(1, n+1), and in Haskell using: take n (iterate (1+) 1).
What is the right OCaml idiom for this?
38
votes
11
answers
135k
views
Using regular expressions to validate a numeric range [closed]
My input number is an int. But the input number must be in a range from -2055 to 2055 and I want to check this by using regular expression.
So is there anyway to write a regular expression to check ...
1
vote
1
answer
168
views
How do I create a new module type by reusing another type?
I started using this pattern to obviate the need to repeat the complex type everywhere. Now I don't have many repetitions and the idea works.
module type RadixNode = sig
type 'a t
end
module ...
3
votes
1
answer
87
views
Adding C and C++ files to a dune build of ocaml app
I'm trying to port an open source existing ocaml app from Makefiles to dune. The application includes some C and C++ stubs. I tried to add a test.c file in a library like this:
(library
(name cdk)
...
9
votes
2
answers
42k
views
OCaml mod function returns different result compared with %
The modulo function in OCaml mod return results different when compared with the modulo operator in python.
OCaml:
# -1 mod 4
- : int = -1
Python:
>>> -1 % 4
3
Why are the result different?...
1
vote
3
answers
2k
views
How to make a tail recursive function and test it?
I would like to make this functions recursive but I don't know where to start.
let rec rlist r n =
if n < 1 then []
else Random.int r :: rlist r (n-1);;
let rec divide = function
h1::...
13
votes
2
answers
4k
views
An concrete simple example to demonstrate GADT in OCaml?
I've searched around for the concept of GADT in OCaml, why we need it and when to use it, etc.
I understand GADT is not only in OCaml but a more general term.
I've found
What are GADTs?
http://...
5
votes
2
answers
1k
views
ocaml, Functors : dependency injection
In Real World Ocaml Chapter 9 which is about functors :
Dependency injection
Makes the implementations of some components of a system swappable. This is particularly useful when you want to ...
6
votes
3
answers
2k
views
How do I write a function to create a circular version of a list in OCaml?
Its possible to create infinite, circular lists using let rec, without needing to resort to mutable references:
let rec xs = 1 :: 0 :: xs ;;
But can I use this same technique to write a function that ...
1
vote
1
answer
94
views
Why do I get a stack overflow?
I am making a simple program that sums all the numbers to n. I am curious why I get a stack overflow when I don't use parentheses on n-1 and I don't when I use them.
let rec summing n=
if n=0
then ...
3
votes
2
answers
116
views
Reusing polymorphic function in ocaml not working
I was creating a simple concat, and thought that this might work.
(** [foldl fun init lst] the [fun] gets applied, left to right:
{[foldl fun init [e1;e2;e3;e4] -> fun e4 (fun e3 (fun e2 (fun e1 ...
5
votes
1
answer
1k
views
Why does the AVL tree in Map of OCaml use balance factor (height diff) 2 instead of 1?
According to AVL tree wiki
The balance factor is calculated as follows: balanceFactor = height(left-subtree) - height(right-subtree). For each node checked, if the balance factor remains −1, 0, or +...
0
votes
1
answer
85
views
CSRF token could not be verified whenever server restarts
I use OCaml/Dream to create a simple HTML form (No sensitive data at all).
let form_page request =
let _csrf_token = Dream.csrf_token request in
Dream.html (Printf.sprintf {|
<form action="/...