Parsing file into Hashtable in OCaml
I'm trying to learn functional programming and having difficulty expressing a file parsing task functionally.Let's say I have a text file with the following format:val_0: <--- "header"key_0_0...
View ArticlePattern matching on rest of list
I'm trying to pattern match on a list of pairs, where I'm trying to return a list from the list of pair, however I'm having trouble figuring out where to make the recursive call. Without the recursive...
View Articlehow do I count the amount of times a (recursive) function executes itself in...
needing some help (if possible) in how to count the amount of times a recursive function executes itself.I don't know how to make some sort of counter in OCaml.Thanks!
View Articleextracting a substring in a string with some conditions
I have a string (on a single line) and the idea is to iterate the string from a given position in the string, go left character by character and stop when we meet a separator (the separators are in the...
View ArticleHow to make tail function return what I need?
I need to encode list in OCaml. Command: encode ['a','a','b','b','b','c'];; have to return [(2,'a');(3,'b');(1,'c')]Now I have this function:let encode list = let rec encodeHelper list acc = match list...
View ArticleWhat can be done with the "constraint" keyword in OCaml
The OCaml manual describes the "constraint" keyword, which can be used in a type definition. However, I cannot figure out any usage that can be done with this keyword. When is this keyword is useful?...
View ArticleEquivalent of Haskell's $ operator in OCaml
Is there an equivalent to Haskell's $ operator in OCaml, or do I have to rely on brackets? See for example,multiplyByFive 5 + 1 -- 26butmultiplyByFive $ 5 + 1 -- 30
View ArticleUnbound values not making sense
For some reason, I get that raise and = (for the id comparison) are unbound.type id = stringlet rec lookup (id: id) (storage: (id * 'a) list): 'a = match storage with | [] -> raise Not_found | (id',...
View ArticleHow to solve linking problem on Z3 in OCaml?
when I suppose to use Z3 lib in OCamlopen Z3the first line is getting Unbound module Z3, which is not able to solve.the code is able to run correctly when I add#use "topfind";;#require "z3";;open...
View ArticleWhat 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?
View ArticleTail-recursive merge sort in OCaml
I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code:let tailrec_merge_sort l = let split l = let rec _split source left right = match...
View ArticleOcaml exception handling for opening input channel
As a beginner in Ocaml, I have this current working code:...let ch_in = open_in input_file intry proc_lines ch_inwith End_of_file -> close_in ch_in;;Now I would like to add error handling for...
View ArticleHow to zip each individual element from two lists into one list using OCaml
If I have an input of a tuple containing two lists of integers of the same length, and I want my output to be a list of these two lists zipped, after extracting these two lists from the tuple how do I...
View ArticleWhy is the output type bool in this OCaml fun?
I'm sure I'm missing something, but where is the bool?let iter t ~f = let rec loop !t = function |Some element -> ~f element.value; loop element.next |None -> () Expecting unit, have bool in type...
View ArticleExplaining pattern matching vs switch [closed]
I have been trying to explain the difference between switch statements and pattern matching(F#) to a couple of people but I haven't really been able to explain it well..most of the time they just look...
View ArticleDune runtest resulat in error in lib/dune on (inline_tests) with expect_test
I am learning OCaml, I strugled with installing it and Dune on Windows 10 but it finally ran.I started doing a few exercises, getting the lay of the land and everything was working as intended.Then I...
View ArticleHow to use `dune test` for a program which reads files from the local directory?
I have a project built in Dune, with the structure| project_dir -| bin | dune | main.ml -| lib | dune | libfile.ml -| test | dune | test.ml | data.csv The libfile.ml reads the data in data.csv just...
View ArticleOCaml : Library ounit2 not found
I want to run unit test with OCaml and OUnit2I have followed the instructions in this tutorial https://cs3110.github.io/textbook/chapters/data/ounit.htmlHowever, when I rundune buildI get the error$...
View ArticleGet the equivalence of input("What is your name")
In python, I can ask a question to a user in using response = input("What is your name?") print(response)How can I do such thing using Ocaml? I think I can use read_line, but I am not sure how to do it...
View ArticleOCaml online: List.sort compare is not working
I am testing the following OCaml's online environment: https://try.ocamlpro.com/However, my algorithms are failing because of the use of List.sort compare x, where x is a list of integers. I have...
View Article