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 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 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 ArticleDisable automatic undindent for `in` keyword for OCaml in VSCode
I am programming in OCaml using VSCode, but each time I begin a line with the in keyword, the editor automatically unindent the line. This is really annoying and I would like to disable this particular...
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 ArticleIs it possible to know if a text input is from a pipe only
I want to make a program that prompts the user to enter something with an introduction text. Something like "Please write something". But if there is a text from a pipe, the result is displayed...
View ArticleDoes OCaml have general map()/reduce() functions?
In Python map() works on any data that follows the sequence protocol. It does The Right Thing^TM whether I feed it a string or a list or even a tuple.Can't I have my cake in OCaml too? Do I really have...
View ArticleHow do I read this OCaml type signature?
I'm currently experimenting with using OCaml and GTK together (using the lablgtk bindings). However, the documentation isn't the best, and while I can work out how to use most of the features, I'm...
View ArticleHow can I skip a term with List.Map in OCAML?
Suppose I have some code like this:List.map (fun e -> if (e <> 1) then e + 1 else (*add nothing to the list*))Is there a way to do this? If so, how?I want to both manipulate the item if it...
View ArticleConverting a binary tree to a list using fold function in Ocaml
Given:type 'a tree = Empty | Node of 'a * 'a tree * 'a treeUse:let rec tree_fold f e t = match t with | Empty -> e | Node (x, l, r) -> f x (tree_fold f e l) (tree_fold f e r);;to convert a binary...
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 ArticleSplit list into list of lists with a list as a parameter for where to split...
I am new to OCaml and curious as to how to write a function called Seperate_by that takes in two parameters, a list and a list of elements on where to split the original list.For example,Seperate_by...
View ArticleHow to build a Map using a list?
I have a node type like the following :type position = float * floattype node = positionI created those modules for the Map :module MyMap = struct type t = node let compare (a1,b1) (a2,b2) = if a1 >...
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 ArticleOCaml 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 ArticleWhat's the use of this mutually recursive type definition in OCaml?
Below is a valid type definition in OCaml.type t = U of u and u = T of tIs there a use for it?And how to create a value of type t? Or type u?
View ArticleCreate 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 ArticleReversing 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 ArticleGetting the two minimum values from a list in OCaml
I need to write a function twoMin: int list → int * int using foldl that returns the two smallest values (not elements) of a list. You may assume that the list contains at least two elements with...
View ArticlePascal Triangle in OCaml: Get next line as a list
I am trying to get an OCaml function which gives the next line of a Pascal's triangle as a list for example:(next_line: int -> list -> int list)let get_next_line [1;1] returns [1;2;1] and so on.I...
View Article