Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Browsing latest articles
Browse All 186 View Live

How to generate Menhir .automaton files with dune in OCaml?

I'm trying to generate .automaton files I added this to the dune file inside lib/ folder(menhir (flags --explain --dump) (modules parser))Then I run dune build, if I do find . -name '*.automaton', it...

View Article



How to use menhir to parse into a GADT expression?

I have just learned something about GADT in OCaml through Real World OCaml, and want to try to transfer the first little language there into a interpreter, thus using menhir.The ast definition is...

View Article

Is there a function that returns a list of values with specific type in OCaml?

In Haskell, using listify with Data can extract values of a specific type from a deeply nested structure without lots of getters. For example, with the following code:{-# LANGUAGE DeriveDataTypeable...

View Article

Why can't dune recognize ppx_jane?

I am now writing a project in OCaml v4.06, and I have installed ppx_jane v0.11.0. Actually the environment is based on a docker image.Now here is the dune file(menhir (modules parser))(ocamllex...

View Article

LLVM API produces invalid IR for ptr type

I am using LLVM 15 and I am trying to compile a language of my own (pretty much like Pascal in terms of syntax) to LLVM IR and I am using Ocaml.When I try to create a struct and set its body to have...

View Article


Downgrade Dune/Opam OR workaround for dune build

I have just installed ocaml, opam and dune on my computer and tried to run a new project using$ dune init project calculator$ cd calculator/$ code .which creates the project and opens it in VS Code....

View Article

Why does my recursion never terminate in OCaml?

let qrec=Queue.create ()let rec queueaddrec1 n= if n==1 then Queue.add 1 qrec else Queue.add n qrec; queueaddrec1 (n-1)let ()= queueaddrec1 5; Queue.iter print_int qrec;VScode told me the following...

View Article

List.map returns an unexpected type

I'm learning OCaml (OCaml 5.0.0).I defined the function below.let make_pair x ls = List.map (fun y -> (x,y)) ls;;I expected make_pair 1 ["a","b"];; returns [(1,"a");(1,"b")], but actually the...

View Article


Infix operators with 3 parameters ocaml

Is it possible in ocaml to define an operator like the array set function which can be written a.(n)<-v?The OCaml manual explains how to define 2-parameters infix operators but not 3-parameters...

View Article


Fast intersection of two N^4 sets

I'm looking for an algorithm that returns (the length of) the intersection of two given N^4 lists / sets (more precisely in [|0;7|]^4). I'm not expecting an answer in a specific programming language...

View Article

OCaml for-loop seems to be re-executing more than expected

I have the following functionlet update s = for i=0 to ((Array.length s) - 2) do for j=0 to (Array.length (s.(i))) - 2 do (s.(i)).(j) <- (s.(i).(j)) + 1; done; doneand it seems to increase the...

View Article

How to write sublist of a char list as [’1’; ’2’; ’3’; ’’; ’’; ’4’; ’5’; ’’;...

I am currently learning OCaml and i couldn't make sublist even thought I can make a list without spaces as ['1'; '2'; '3'; '4'; '5'; '6']. I couldn't find any sources to my question so thanks for...

View Article

z3 OCaml module linking error in "dune utop"

I have to execute z3 module in OCaml.But when I try to execute dune utop then I meet under errormingyu809@DESKTOP-NTK8E0T:~/ocaml/COSE419-2024-main/hw2$ dune utopFile "_none_", line 1:Error: Error on...

View Article


How to convert non-built-in types to string in Ocaml?

I'm trying to write a module that convert something like x:int = if true then 3 else 5 to a stringHere's the code I have so farmodule Ast =structtype typ = Bool | Inttype var = A | B | C | D | E |...

View Article

Create a tuple list from a list using fold_left

How to create a tuple list from one single list, like so:[1; 2; 4; 6] -> [(1, 2); (4, 6)]I want to do it using function List.fold_left since I'm trying to learn that currently but don't know how......

View Article


Reversing tuples inside a list with fold_left in Ocaml

Let lst be a List with tuples which I want to reverse each (The order of the tuples needs to be the same). For example:[(a,b);(c,d)] -> [(b,a);(d,c)] I know that it can be done with map:List.map...

View Article

Interleaving in OCaml

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...

View Article


Compiling OCaml .cmm file

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...

View Article

Write a function that returns every other element of the list

I want to write a function that can return every other element of the list like this ['a' ; 'b' ; 'c' ; 'd' ; 'e'] can return: ['a' ; 'c' ; 'e']My function can use only with predefined functions...

View Article

List.filter and List.mem [closed]

I am having a problem understanding this particular linelet lst = ["a", "b", "c"];List.filter (fun (a, _b) -> not (List.mem a lst)) assoc

View Article

this pattern-matching is not exhaustive in OCaml

I am new in OCaml and I wrote some code to get the n element of a listlet rec n_elem l n = match n with| 0 -> match l with | h::_ -> h | _ -> failwith "erorr with empty list"| _ -> match l...

View Article


Glade and Ocaml?

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...

View Article


OCaml map of int keys :: where is the 'simple' int module to use with the...

I need an OCaml map with keys of type int so I am using Map.Make to create one. However it seems that the standard modules'only' provide modules like Big_int, Int32, Int64 and Nativeint which require...

View Article

Is enumeration over custom union type possible?

Having a disjoint union type like this:type yolo = | A | B | CIs it possible in OCaml to iterate/enumerate over each possible value of type yolo without prior knowledge of how many/what these values...

View Article

opam install ocaml-lsp-server is not working

trying to install, but getting the following message. Constructing initial basis... The following dependencies couldn't be met: - ocaml-lsp-server → dyn → ocaml >= 4.08.0 base of this switch (use...

View Article


Does OCaml support for-each loops?

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...

View Article

How can I force a record literal to have a type in OCaml? [duplicate]

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...

View Article

Why is type-level function application written in reverse order compared to...

In OCaml, function application syntax has the function first and argument second:f 123But for functions at the type-level, it's written in the opposite order:let x : int option = NoneIf option is...

View Article

Function calling for each element in a list

Using only recursion (ie. no loops of any sort), given a list of elements, how can I call a function each time for every element of the list using that element as an argument each time in OCaml? Fold...

View Article



(Lazy) Haskell undefined/bottom in OCaml

Haskell has a really swell undefined value, which lazily raises an exception (upon evaluation). Ocaml is of course strict, so as far as I can tell there is no equivalent of Haskell's undefined. This is...

View Article
Browsing latest articles
Browse All 186 View Live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>