Using foldl to get the mean of a list
I am trying to get the mean of a list by using the following helping function:let rec foldl f l b = match l with | [] -> b | x :: l -> foldl f l (f x b);;let avg l = foldl (fun x acc -> let x...
View ArticleHow to push a synthetic token (which is not related to any symbol) in Menhir...
I am parsing the Elm lang-like syntax. And look at the pattern matching (inside pattern matching)case a of 1 -> case b of"hello" -> 'h' _ -> 'w' _ -> 'e'We see there is a significant...
View ArticleDeclaring foldl ( from left) in terms of foldr (from right)
I have foldl:let rec foldl f lst acc = match lst with | [] -> acc | hd::tl -> foldl f lst (f hd acc)and I have foldr:let rec foldr f lst acc = match lst with | [] -> acc | hd::tl -> f hd...
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 ArticleMake OCaml function polymorphic for int lists and float lists
Is there a way to create a polymorphic add function in OCaml that works equally well for ints and floats? So for example if I have a function like:partialsums [1; 2; 3; 4; 5] I should get [1; 3; 6; 10;...
View ArticleReturning an unnamed sum type
I am trying to create a local sum type inside of a function and then return said sum type without it being declared in main. Is this possible? I don't believe it is directly, but I have been playing...
View ArticleWhat (exactly) are "First Class" modules?
I often read some programming languages have "First Class" support for modules (OCaml, Scala, TypeScript[?]) and recently stumbled upon an answer on SO citing modules as first class citizens among the...
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 ArticleCreate a natural implementation for f2 : (( ’ a ->’b ) ->’a ) -> ( ’ a ->’b )...
I cannot use library functions.I am trying to create a function with type:(('a -> 'b) -> 'a) -> ('a -> 'b) -> bI have tried:let f2 f = fun g -> f (fun x -> g x)But the type of this...
View ArticleOCaml used in demonstrations?
I'm looking for examples of usages in OCaml to demonstrate simple properties or theorems.An example may be, given an ocaml definition of binary trees, demonstrate that the maximum number of nodes is...
View ArticleLexical vs dynamic scoping
Would the OCaml expression let x = 5 in (let x = 6 in x) evaluate differently for dynamic vs lexical (static) scoping?when would an expression evaluate differently for dynamic vs lexical scoping...
View ArticleHow to initialize Map from List in OCaml?
Using Map.empty and Map.add is verbose, is there something like:Map.of_list [(1,"A"); (2,"B"); (3,"C")];;
View ArticleMy Ocaml parsing function gives me an expression error
I'm working on parser function consumes tokens from lexer function to produce an abstract symbol tree (AST) for a MicroCaml expression.parse_exprType: token list -> token list * exprDescription:...
View ArticleIs is possible to tell ocamlformat to ignore a specific portion of code
Is it possible to make ocamlformat ignore a specific portion of code by inserting some specific comments?Here's what I tried, without success, using information found online:(* @@disable *)let...
View ArticleAssociate AST nodes with token locations in source file in menhir
I am using Menhir to parse a DSL. My parser builds an AST using an elaborate collection of nested types. During later typecheck and other passes in error reports generated for a user, I would like to...
View ArticleKeepsorted function
I'm trying to solve a problem on a function called "keepsorted";This function has to keep a sub-list of growing number form a list called l1.For example if I have let l1 = [1; 2; 0; 1; 2; 4; 3; 5; 3;...
View ArticleWhy does ocaml keep miss understanding type?
module Value =struct type t = Int of intendmodule M = Map.Make(String)type expr = | Num of int | Add of expr * exprtype t = Value.t M.t (* Value.t is Int of int *)let rec add_map (st: string list) (e:...
View ArticleHow can I change user defined type to int?
type t=Num of int (* need to change to Num2 of int *)type e = Num of int| Add of e*elet rec intp e : t =| Num n -> t (n)| Add (e1, e2) -> int (intp e1) + int (intp e2)I want to add two variable...
View ArticleVariable Hiding in OCaml's Pattern Matching
I have a method that returns the count of the most common element in a list (specifically a list of char values)Here's my code:let getMostFrequentCharCount li = let hashTable = Hashtbl.create...
View ArticleOCaml list issue with cons operator
Here is my code:let rec intersection l1 l2 res = match l1 with | [] -> res | h1 :: t1 -> ( match l2 with | [] -> [] | h2 :: t2 -> if member h1 l2 = true then intersection t1 l2 h1::res else...
View Article