printing a 2D array in ocaml
I have written following code in Ocaml to print 2D array:let string_of_float_arr l = String.concat " " (Array.map string_of_float l)let float_arr_to_string l = String.concat "\n" (Array.map...
View ArticleHow to compute the average of a list of a special record in OCaml?
Lets say we have a record which defines students:type student = { name : string; age : int; grades : (float) list;}And safe them in a list like this:let studentlist = [ {name="alex"; age=7;...
View ArticleVerify that an OCaml function is tail-recursive
How can I tell if OCaml recognizes a particular function as tail-recursive? In particular, I want to find out if the OCaml compiler recognizes Short-circuited operators and tail recursionThanks to...
View ArticlePolymorphic exception arguments in OCaml
In OCaml, one can define their own exceptions and these can take arguments, as in the following snippet.exception there_is_a_problem of stringI wonder if there is a way to use exceptions that are...
View ArticleOCaml analogue to Python's String join method
Given a list of string elements in Python, I can concatenate these elements with specified glue. For example:''.join(["phd", "in", "everything"])evaluates to the string "phd in everything".What...
View ArticleEliminate list elements contained in another list
I have this code for eliminate a element where is contained in a list:let remove_all x l = let rec aux l acc = match l with [] -> List.rev acc | h::t when h=x -> aux t acc | h::t -> aux t...
View ArticleNon-exhaustive pattern matching issue
I made a dummy function that takes in a list of two lists, as shown below:# let rec test ([a;b]) = match [a;b] with [] -> [] | h::t -> if ((List.length h) > 0) then [List.hd a] else [];;And I...
View ArticleList.assoc using List.find
I want to implement the List.assoc function using List.find, this is what I have tried:let rec assoc lista x = match lista with | [] -> raise Not_found | (a,b)::l -> try (List.find (fun x -> a...
View ArticleOCaml: pell function with int lists
I am trying to write a simple function in OCamllet rec pell (i: int) =(if i <= 2 then i (*if given n is less tahn 2 then return 2, else return previous n-1 th term and n-2 nd term recursively*) else...
View ArticleFunction doesn't print correctly
So I have to create a "lights out" game. I have to create two functions. One function "flip"val flip : bool array array -> int -> int -> bool array array = <fun>that given a bool matrix...
View Articleocaml tail-recursive functions
I'm running Ocaml using utop, when I run the below function on a very long input:let string_to_list str = let rec loop i limit = if i = limit then [] else (String.get str i) :: (loop (i + 1) limit) in...
View ArticleHow to figure out the type of this OCaml function?
I try to figure out the type of the OCaml function below:let subst = fun x -> fun y -> fun z -> x z (y z)(* Or more concisely *)let subst x y z = x z (y z)Here's my derivation.Step 1, assume...
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 ArticleOCaml: How do I provide fully-qualified module names to avoid name collisions?
I'm working in a package Foo with a module called Base, but I'd also like to use the OCaml library Base. How can I distinguish between the two in open statements?E.g., is there a module-root specifier...
View ArticleA typical bracket balanced problem in ocaml
I'm trying to create a function which returns a boolean value to see if the string is balanced. But the result is just not what I expect. Can someone point out what I did wrong?For example:false =...
View ArticleHow to figure out the type of this function?
I try to figure out the type of the OCaml function below:let subst = fun x -> fun y -> fun z -> x z (y z)(* Or more concisely *)let subst x y z = x z (y z)Here's my derivation.Step 1, assume...
View ArticleWhy the number of function parameters doesn't match the function definition?
In below snippet, the recursive function lookup has only one parameter.How could it later be applied with two parameters k and t?let rec lookup k = function (* only 1 parameter k *)| [] -> None|...
View ArticleAre there functional programming languages that run on the GPU?
Using the traditional, sequential reduction approach, the following graph is reduced as:(+ (+ 1 2) (+ 3 4)) ->(+ 3 (+ 3 4)) ->(+ 3 7) ->10Graph reductions are, though, inherently parallel. One...
View ArticleWrapping my head around OCaml
I'm only a novice programmer (I do it for fun) and I'm coming from the world of Python/C++/other procedural languages, and procedural style of problem solving. I fell in love with OCaml's simplicity...
View ArticleStack performance in programming languages
Just for fun, I tried to compare the stack performance of a couple of programming languages calculating the Fibonacci series using the naive recursive algorithm. The code is mainly the same in all...
View Article