How to process elements of an OCAML list?
I want to process the data present in file "persons.txt".But i have tried everything to process all the lines from text file.The only way i can process data is by creating the list manually.let myList...
View ArticlePair swap elements in list using Ocaml
I am a Ocaml beginner, and am not able to understand tail recursiveness or list iteration. How can we iterate through list is 2s and swap the pairs?let rec swap = function| a :: (b :: _ as t) ->...
View ArticleImplementation of List.of_seq
If we look at the source for the OCaml List module, of_seq is defined as:let[@tail_mod_cons] rec of_seq seq = match seq () with | Seq.Nil -> [] | Seq.Cons (x1, seq) -> begin match seq () with |...
View Articlestring to list of char
I want to write a function that taking a string and return a list of char. Here is a function, but I think it is not do what I want ( I want to take a string and return a list of characters).let rec...
View ArticleWhy does the last "else" not restart the recursion with the new values?
So im learning Ocaml at University and this year the code we create have some special rules : no loops or arrays(i dont know why but she almost seemed angry when a friend of mine used them at the exam...
View ArticleNarrow down a type of record with explicit field declarations
I'm facing a problem that I need to narrow down the typetype 'a poly = {field1: 'a; field2: int}I want to declare a new typetype mono = string poly = {field1: string; field2: int}But when I try to do...
View ArticleStack overflow during compilation on large list literal in OCaml
I have a small project for checking if a number is prime. The idea is to prepare (generate) a list of primes up to a certain point for the library function to use (use a code generator for that).Whilst...
View ArticleQuestion about partial application in OCaml
I have a question about OCaml:Assume the following function declarations:let secret x y = let secret' x y z = x + y - z in let z = if x mod 2 = 0 then y else x - y in secret' x z ;;print_int (secret 3...
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 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 ArticleFlattening a Nested List in OCaml
I'm doing problem 7 of the OCaml exercises. It basically asks given a nested list as defined:type 'a node = | One of 'a | Many of 'a node listWrite a function function flatten that flattens it:assert (...
View ArticleJust want choose some random elements of a list and return them in OCaml
I want to choose n distinct random elements in a list l and return them in choose_elements but I have a StackOverFlow error for sufficiently large lists!I tried to use a tail_recursive function...
View ArticleWhy do multiple let statements inside a while body give a compile error?
I'm learning OCaml and I'm struggling with this snippet:let found = ref falselet () = while not (Queue.is_empty q) && not !found do let next = Queue.pop q let () = found := (next.subtotal ==...
View Articlelength function without recursion ocaml
I'm trying to rewrite the List.length function without using recursion. Here's my code:(* given *)type 'a list = | [] | (::) of 'a * 'a listlet nil : 'a list = []let cons (hd : 'a) (tl : 'a list): 'a...
View ArticleComposition of function pair
I am very new to OCaml and I am currently trying to solve some exercises. While doing so I stumbled over the following problem:let compose_pair (p:(('b -> 'c) * ('a -> 'b))) : 'a -> 'c = The...
View ArticleWhy the same function prints different output?
I have defined the following module to implement a matrix type:module MatrixImplementation: MatrixADT.MatrixInterface = struct type 'a matrix = {n: int; m: int; c: 'a array array};; let zeroes n m =...
View ArticleHow to use let* Monad syntax for Lists
I'm playing around with OCaml's let* syntax but I can't get anything to work when working with Lists.When I try to define a (not very useful) dupe function like this:let (let*) = List.concat_maplet...
View ArticleOCaml `Map.Make` input module
I am following the example here.module IntPairs =struct type t = int * int let compare (x0,y0) (x1,y1) = match Stdlib.compare x0 x1 with | 0 -> Stdlib.compare y0 y1 | c -> cendmodule PairsMap =...
View ArticleHow to sort a list of tuples using both key and value in OCaml?
I have a list of tuples like (1, "a"), (2, "b"), (3, "c") and I want to sort them like1 a2 b3 bWhere the numerical order takes precedence first and then the alphabetical order. Like if I have(1,...
View ArticleWhat's wrong with my attempt to add 1 to every element in my list in Ocaml?
I don't understand the error message I'm getting or what's wrong with what I'm trying to doI just want to use List.fold_left to apply my add1 function to this list [1,2,3]My add1 function should just...
View Article