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

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 Article



Pair 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 Article

Implementation 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 Article

string 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 Article

Why 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 Article


Narrow 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 Article

Image may be NSFW.
Clik here to view.

Stack 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 Article

Question 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 Article


Sorting 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 Article


Difficulties 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 Article

Flattening 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 Article

Just 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 Article

Why 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 Article


length 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 Article

Composition 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 Article


Why 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 Article

How 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 Article


OCaml `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 Article

How 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 Article

Image may be NSFW.
Clik here to view.

What'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
Browsing all 186 articles
Browse latest View live




Latest Images