OCaml » Stack Overflow
39 FOLLOWERS
Stack Overflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers. OCaml is a strict statically-typed functional programming language, focusing on expressivity, correctness, and efficiency.
OCaml » Stack Overflow
9h ago
In OCaml, function application syntax has the function first and argument second:
f 123
But for functions at the type-level, it's written in the opposite order:
let x : int option = None
If option is thought of a function at the type-level, then why is it not written like this?
option int
Is there a technical or perhaps historical reason ..read more
OCaml » Stack Overflow
9h ago
I have some record types with overlapping fields. This causes a type mismatch:
type apple =
{
price : int
}
type banana =
{
price : int
}
Is it possible to force the type of a record literal to be one or the other?
(Hypothetical syntax)
{
apple.price = 123
}
{
banana.price = 123 ..read more
OCaml » Stack Overflow
9h ago
I would like to print each integer in a list.
I can do this with List.iter like so:
digits
|> List.iter
(fun i -> print_int i; print_newline ())
However, I generally prefer to keep imperative code outside of pipelines of functions.
Is it possible to write this in a for-each loop in OCaml?
(Hypothetical syntax)
for i in digits do
print_int i ;
print_newline ()
end ..read more
OCaml » Stack Overflow
5d ago
Having a disjoint union type like this:
type yolo = | A | B | C
Is it possible in OCaml to iterate/enumerate over each possible value of type yolo without prior knowledge of how many/what these values are?
What I want to achieve is a function that takes a type and returns a list with all of its values. For example:
let yolos = extractor yolo
val yolos : yolo list = [A; B; C]
(I am looking for a way to implement extractor function from the above snippet)
In F# my goal is achievable with something similar to this:
open Microsoft.FSharp.Reflection
type yolo =
| A
| B
| C
let yol ..read more
OCaml » Stack Overflow
5d ago
I'm currently working on a project in Ocaml (I love this language), and I need to create a graphical interface. I've tried LabelGTK, it works but it's REALLY tiring... As a regular Glade user, I'd like to be able to use it, but I can't find anything on it... Just an old page from 2009 that gives an example code but without the library that goes with it...
I know that using an xml file to create a GUI in Ocaml is a bit of a shame (since Ocaml is statically typed and reading an unsafe, modifiable file into it is a bit counter-productive), but I'd like to not have to bother with LabelGTK...
Thank ..read more
OCaml » Stack Overflow
1w ago
I've recently been learning about compilers, more specifically Ocaml ones, and I was wondering how does the compiler actually determine which regex a string matches. Does it build a DFA for each regex defined by the lexer and then test each string on each DFA until it finds a match or is there some other algorithm/intuition.
Couldn't find a clear answer online ..read more
OCaml » Stack Overflow
1w ago
I wrote simple OCaml code like:
let rec fib(n: int) =
match n with
| 0 -> 0
| 1 -> 1
| _ -> fib(n-1) + fib(n-2)
With -dcmm option to ocamlopt, I could generate .cmm files following:
cmm:
(data)
(data int 3063 "camlTest__1": addr "camlTest__fib_267" int 72057594037927941)
(data int 1792 global "camlTest" "camlTest": int 1)
(data
global "camlTest__gc_roots"
"camlTest__gc_roots":
addr "camlTest"
int 0)
(function{test.ml:1,11-88} camlTest__fib_267 (n/268: val)
(if (!= n/268 1)
(if (!= n/268 3)
(+
(+ (app{test.ml:5,9-17} "camlTest__fib_267" (+ n/268 -2) val ..read more
OCaml » Stack Overflow
1w ago
I am trying to create a function which interleaves a pair of triples such as ((6, 3, 2), ( 4, 5 ,1)) and create a 6-tuple out of this interleaving. I made some research but could understand how interleaving is supposed to work so I tried something on my own end ended up with a code that is creating a 6-tuple but not in the right interleaved way. This is my code
let interleave ((a, b, c), (a', b', c')) =
let sort2 (a, b) = if a > b then (a, b) else (b, a) in
let sort3 (a, b, c) =
let (a, b) = sort2 (a, b) in
let (b, c) = sort2 (b, c) in
let (a, b) = sort2 (a, b) in
(a ..read more
OCaml » Stack Overflow
1w ago
I have to execute z3 module in OCaml.
But when I try to execute dune utop then I meet under error
mingyu809@DESKTOP-NTK8E0T:~/ocaml/COSE419-2024-main/hw2$ dune utop
File "_none_", line 1:
Error: Error on dynamically loaded library: /home/mingyu809/.opam/default/lib/lwt/../stublibs/dllz3ml.so: /home/mingyu809/.opam/default/lib/lwt/../stublibs/dllz3ml.so: undefined symbol: Z3_mk_u32string
But I execute dune build and dune exec -- ./main.exe, it works well.
I use WSL2 on Windows 11
I tried to remove opam and other package and reinstall but same error occurs ..read more
OCaml » Stack Overflow
1w ago
I'm trying to write a module that convert something like x:int = if true then 3 else 5 to a string
Here's the code I have so far
module Ast =
struct
type typ = Bool | Int
type var = A | B | C | D | E | F
type exp = Const of int * typ
| App of string * exp list
| If of exp * exp * exp
| And of exp * exp
| Or of exp * exp
| Id of var * typ * exp
let rec toString (t) =
let formatDec1(va,ty,e) = ???
match t with
Const(n, _) -> print_int n
| App(id, [e1; e2]) -> formatter(" " ^ id ^ " ", e1, e2)
| App(id, [e1]) -&g ..read more