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

Sorting by alphabetical order

I would like to create a function which will add a registration number plus a certain negative time. Here is an example : # enter_car "DEF456" (−4) [("ABC13", −2); ("GHI789", −3)];;− : (string∗int)...

View Article


OCaml initializing list in loop for

I am a beginner with OCaml. I would like to skip the first element of my list.Here is my list:let l = [1;2;3;4;5;6;7;2;1];;I want to use this in my FOR:let l = List.tl l;here is my full code:let l =...

View Article


OCaml matching tuples? Why is this match case unused?

I have the following bit of code in OCaml: let matchElement x y= match x with | (y,_) -> true | _ -> false;;and I'm getting a warning that the case _ will always be unused. My intention was that...

View Article

How to delete all occurrences of an item in a list?

So...I have a few problems with that,my goal is to delete items from a list as follows.For example,I have a list like that:let list = [1;2;4;5;1;1;6]And when i do some like this:remove_from_list 1...

View Article

Why can I not print this input a second time in OCaml?

I am very new to OCaml and am attempting to learn and write a program at the same time. I am writing a palindrome program. I am attempting to get a string from the user such as d e v e d or Hello...

View Article


Checking if all elements are equal with map/fold

Given a list of elements, like [1, 1, 1] or ["a", "a", "a"], how can I check if they're all equal using map/fold?I tried to do something like this:let eq lst = fold (=) lstwhich doesn't compile because...

View Article

Can someone explain OCaml Tail Recursion Like I am five?

I cant wrap my brain around tail recursion specifically in ocaml also explain why one calls the "in" function at the end. P.S. I am talking most basic tail recursive function.

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


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


Tail call optimization with a function returning a tuple

I have a simple function that splits a list at an index:let rec split_at ls i = match i with | 0 -> ([], ls) | _ -> match ls with | [] -> raise Not_found | h::t -> match split_at t (i - 1)...

View Article

How to apply a function in an iterable list

so I am new to OCaml and im having some trouble with lists.What I have is a List of chars as follows:let letters = [a;b;c;d]I would like to know how can I iterate the list and apply a fuction that...

View Article

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

Extending OCaml Module

Is there any way to add functions to an already existing module in OCaml? Specifically, I have some module Loc that tracks locations in a source file. I also have a module called Ast that represents...

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

What does (string * string) list -> (string -> string) mean?

This might be a super dumb question, but I don't get it.What does(string * string) list -> (string -> string) mean?Especially the last part (string -> string). How can you achieve that?I...

View Article


Why does the OCaml function `read_line` have type `unit -> string`?

I understand that its return type is the string that is read but why does it need the parameter of type unit?

View Article

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


Ocaml extlib-1.4 installation

I've tried to install extlib-1.4 but got an error(i'm noob)I typed ocaml install.ml and got:File "bitSet.ml", line 23, characters 40-53:23 | let bcreate : int -> intern = Obj.magic String.create...

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
Browsing latest articles
Browse All 501 View Live


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