Why does 'opam init' fail claiming 'Missing dependencies' even though they...
I'd like to install OCaml on my platform.I followed the official OCaml install instructions detailed here: https://ocaml.org/docs/installing-ocaml, running:bash -c "sh <(curl -fsSL...
View ArticleDoes 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 ArticleHow 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 ArticleWhy 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 ArticleFunction 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 ArticleHow to write a function to count the number of elements in a list using a Map?
module IntMap = Map.Make(struct type t = int let compare = compare end)let rec count_with_map (il: int list) = match il with | [] -> [] | head :: tail -> match count_with_map tail with | (c, n)...
View ArticleIf statements questions in OCaml
I am trying to write the following code in OCaml:let a = 0let b = 1 if a > b then { print_endline "a"; print_endline "a"; }print_endline "b"And then I encountered the following error:File "[21]",...
View ArticleSorting a list of lists according to length of sublists
let rec insert cmp e = function | [] -> [e] | h :: t as l -> if cmp e h <= 0 then e :: l else h :: insert cmp e tlet rec sort cmp = function | [] -> [] | h :: t -> insert cmp h (sort cmp...
View ArticleWorkaround to append attaching elements in reverse order when sorting
I want to sort so that odd numbers in a list appeart first and evens appear last, but i need evens to be the same position to how they were pre sort, is there a simple workaround to this?let rec...
View ArticleUnbound Module when using Dream eml pre-processor
I'm trying to learn OCaml by way of building a website with this lovely library I found called Dream (https://aantron.github.io/dream/), but I've run into an issue when trying to work with the template...
View ArticleComputing a set of all subsets (power set)
I am trying to get a function (given as a parameter a set) to return a set whose elements are all the subset formed from the main set.Ex: {1;2;3} -> { {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}...
View Articleocaml begin/end pattern matching
Haven't been able to find much online documentation regarding begin/end in ocaml. I have two different pattern matches in the same function (which I want to be independent of each other), but vscode is...
View ArticleConverting PCL to PDF
I am looking to create (as a proof-of-concept) an OCaml (preferably) program that converts PCL code to PDF format. I am not sure where to start. Is there a standardized algorithm for doing so? Is there...
View ArticleOcaml merge Hash tables
I would like to merge two same-type hash tables in Ocaml, so that the information of both of them gets stored in a single table. Imagine, something like: type tabType = (string, variable) Hashtbl.t let...
View ArticleFlush an OCaml formatter without resetting the pretty printer state
In the Format module, pp_print_flush writes the output to the formatter, AND resets the pretty printing state. Is it possible to flush, but not reset the state?For example, creating a formatter with...
View ArticleOcaml: Lazy Lists
How can I make a lazy list representing a sequence of doubling numbers? Example:1 2 4 8 16 32
View ArticleDifficulties with Ocaml. Lists
I wrote this function which was supposed to give me the before last element of a list, but it doesn't work? Do you know why? thanks!let rec exo1= match l with |List.length l = 0 or 1 -> failwith...
View Articlehow to write a function to find max number in a list of type number in ocaml?
how to write a function that returns the maximum number from a list of number values, if one exists. In the case it is given the empty list then it cannot return a number. i get:let rec max_number_list...
View ArticleSaving a large integer in Ocaml
I am trying to convert a large base-10 number to base-16 and then to hex. The error I am getting is Ocaml is saying the integer is too big.utop # let large_int =...
View Article