Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Browsing latest articles
Browse All 517 View Live
↧

Why is the parameter implicitly declared in ocaml

I am learning OCaml to get into functional programming. I am solving the beginner exercises.This is the solution of the exercise to get the nth element in a list.let rec at k = function | [] -> None...

View Article


If statements questions in OCaml

I am trying to write the following code in OCaml:let a = 0let b = 1 if a > b then { print_endline "a"; print_endline "a"; }print_endline "b"And then I encountered the following error:File "[21]",...

View Article


How to fold on x elements of list per fold

So, let us say we have some list like as follows: [1; 2; 3; 4; 5; 6], and let us say that I want to fold on 2 elements per call of the function.So, I would apply the function on (1, 2), (3, 4), and (5,...

View Article

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 Article

How to divide a list of characters (that represents a word) into all possible...

I defined a type "word" as a list of characters. I am trying to write a recursive function "divide_word" that takes one "word" as an argument and returns a list of triplets of all possible (prefix,...

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

If i have a list of pairs ex. (String, int) how to add every int in strings...

i have this problem where i have a list of pairs string int and i want to sum the total of ints with the same String ex:list -> [("a",1);("b",1);("a",1);("c",1)] should returnlist ->...

View Article

Pattern matching on rest of list

I'm trying to pattern match on a list of pairs, where I'm trying to return a list from the list of pair, however I'm having trouble figuring out where to make the recursive call. Without the recursive...

View Article


OCaml How to reverse only even values in list

I want to transefer something like this:[0,1,2,3,4,5,6,7]into[6,1,4,3,2,5,0,7]

View Article


How to take product of two list in OCaml?

I have two lists :let a = ["a";"b"];let b = ["c";"d"];I want an output list c such as :c = ["a";"c";"a";"d";"b";"c";"b";"d"];How to do it in ocaml as lists are immutable? I am new to it.

View Article

How do I create a dictionary in OCaml that associates to each element of the...

I have two lists, ["0","1"] and ["0","1","0"], and I want to get a list, [(0,2),(1,1)], - which associates to each element of the first list the number of its occurrences in the second list. I tried...

View Article

Iterating through a string using For loop

I am trying to iterate through the characters of string using for loop however I get the following errorlet str = "Hello"for var=0 to (String.length str -1) do let temp = String.get str vardone;;Error...

View Article

Ocaml function that from a matrix produces a modified matrix

So i have this little function:let switch matrix = for i = 0 to Array.length matrix - 1 do for j = 0 to Array.length (Array.get matrix i) - 1 do if Array.get (Array.get matrix i) j = false then...

View Article


Ocaml string histogram

I'm trying to do an Histogram from String in OCaml.I'm at this state, editing a little this: create a histogram OCaml to use a String input.How can I see the content of charFreqs, or does it...

View Article

how to translate `if b < a` condition to match pattern?

Like the if .. then in code# let rec range2 a b accum = if b < a then accum else range2 a (b - 1) (b :: accum);;how to write this b < a as a pattern in match .. with ? sth likelet rec range2 a b...

View Article


Solving OCaml Problems

Define a function sign which given an integer returns 1 if it is positive, -1 if it is negative and 0 if it is zero.my solutionssolution 1let sign i = if i!=0 then (if i<0 then -1 else 1) else...

View Article

Ocaml parser for applying a function

I have a file, parser.mly, which has the following%{open Ast%}(* Ast holds the abstract syntax tree type definitions. *)%token EOF%token OR%left OR%start <Ast.prog> prog%% prog: | e = expr; EOF...

View Article


How can I open List without importing the type List.t?

I'm using OCaml 5.3.0.I like to write open List at the top of my source files, since I use list functions so frequently that I don't want to have to prefix them as e.g. List.length or even L.length....

View Article

How to debug OCaml dune test program?

Let's say I:dune init proj foobarcd foobarThen modify test/test_foobar.ml to be:let () = print_endline "First"let () = print_endline "Second"let () = print_endline "Third"I can execute the program with...

View Article

Why does `let f : int -> int list -> int list = (::);;` fail?

I would think that OCaml would read the following as an instance of partial application, and store the function as f. However, the compiler complains that the function is applied to too few arguments....

View Article

let in and the use of ; vs ;;

I have this chunk of code below. I'm a bit confused as to how the let in syntax should work. I was under the assumption that the let in let you use that variable in the scope of in. Although, I don't...

View Article


How to print a list of lists

I'm trying to print a specific list, but it doesn't worklet rec listes_paires l = match l with | [] -> [] | x :: r -> if List.length x mod 2 = 0 then (x :: (listes_paires r)) else ((x @ x) ::...

View Article


Base won't load in utop

So basically I am trying to follow these instructions https://dev.realworldocaml.org/install.htmlI followed them literally. I have a working utop. I have a working VS Code environment. I can run code...

View Article

OCaml Option get

I'm new to OCaml, I'm trying to understand how you're supposed to get the value from an 'a option. According to the doc at http://ocaml-lib.sourceforge.net/doc/Option.html, there is a get function of...

View Article

Time complexity of :: and @ (OCaml)

I was reading this and was wondering if :: is always more efficient than @ or if only in that particular implementation of revlet rec rev = function | [] -> [] | h::t -> rev t @ [h]let rev l =...

View Article


Formatting numbers with thousand separators

Is there anything in the standard library or in Core that I can use to format integers with thousand separators?

View Article

How to turn a set of sets to list

So I have two modules, for ex.module A = Set.Make (struct type t = ... let compare = Stdlib.compareend)module B = .... (virtually the same thing)Now the question, if I have n amount of elements of type...

View Article

OCaml function parameter pattern matching for strings

I tried to pass a string in to get a reversed string. Why can't I do this:let rec reverse x = match x with | "" -> "" | e ^ s -> (reverse s) ^ e;;The compiler says it's a syntax error. Can't I...

View Article

find longest repeating sequence in list

If I have a List like this:[i;i;i;a;b;b;a;i;i;c] (*the longest repeating sequence would be [i;i]*)[i;i;i;i] (*here the max_pattern would be [i;i] (has to repeat, no overlapping*)[t;f;f;t] (*here it...

View Article



Reading integers from file OCaml

I have to read integers from a "file.txt" in OCaml and store them in a list. I have tried to read with stdlib but it does not work. Also I cannot understand how scanf works for files. If someone could...

View Article
Browsing latest articles
Browse All 517 View Live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>