Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Browsing all 518 articles
Browse latest View live

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


Difference between two kinds of recursive function

In OCaml, there are two ways I have seen to write a map function for examplelet rec map f xs = match xs with | [] -> [] | x::rest -> f x :: map f restandlet map f xs = let rec go xs = match xs...

View Article


How do I read in lines from a text file in OCaml?

This is what I have so far. Isn't this all that you need? I keep getting the error "Error: Unbound module Std"let r file = let chan = open_in file in Std.input_list (chan)

View Article

this pattern-matching is not exhaustive in OCaml

I am new in OCaml and I wrote some code to get the n element of a listlet rec n_elem l n = match n with| 0 -> match l with | h::_ -> h | _ -> failwith "erorr with empty list"| _ -> match l...

View Article

Write a function that returns every other element of the list

I want to write a function that can return every other element of the list like this ['a' ; 'b' ; 'c' ; 'd' ; 'e'] can return: ['a' ; 'c' ; 'e']My function can use only with predefined functions...

View Article


How to add user input values to an array in Ocaml

I am working in OCaml and have to do the following problem7.1 Problem 1 – Number of values less than averageInput: An integer, listlen, followed by listlen number of integer values.Output: The list of...

View Article

Return a list of tuples inside a rectangular range

As a beginner in OCaml, I'm trying to write a function who takes two int arguments (a and b), and should return a list which contains all tuples (i,j) where i is between 0 and a, and j is between 0 and...

View Article

Unbound module Graphics Ocaml on Linux mint

i have read multiple identical problem but answers dont fix it.The OCaml toplevel, version 4.11.0+dev0-2019-10-18opam list# Packages matching: installed# Name # Installed # Synopsisbase-bigarray...

View Article


How to structure an accumulator for fold_left with index access

I want to write a function that takes a List [a_n; a_n-1; ...; a_0] with an accumulator acc.The function is supposed to calculate the sum of every element in the whole list raised to the i'th power....

View Article


OCaml function with data type tree

We are give a tree that contains two types of elements. It is defined with the following data-structure.type ( 'a , 'b ) tree = Empty | Vertexa of 'a * ( 'a , 'b ) tree list | Vertexb of 'b * ( 'a , 'b...

View Article

How to modify a list of lists if it contains x or -x?

I'm trying to modify a list of lists with certain conditions, if a nested list contains x, I'd like to remove that list from the list, if a nested list contains -x, I'd like to remove -x from that list...

View Article

Recursively shorten a string in OCaml

Hey would anyone be able to help me create a function in OCaml that would take in a string and recursively return the string with less and letters. I am trying to use the sub string and recursion to...

View Article

Trying to replicate the elements in a list n times in OCaml

I'm trying to write a function that would take an input like :repeat 3 [1;2] ;;and display something like:[1;2;1;2;1;2] Now the code I have is:let repeat ls n = let rec helper acc n l = if n = 0 then...

View Article


How to execute OCaml code from Scala/Java?

I'm about to write an abstract syntax for OCaml in scala, in combination with a pretty printer.After that I want execute the generated OCaml code from scala and work with the result.Is there a way to...

View Article

Can I simplify this recursive concat function using List.fold_left?

I have created a working solution for concat, but I feel that I can reduce this using List.fold_lift.Here is my current code:let rec concat (lists : 'a list list) : 'a list = match lists with | []...

View Article


Check type of variable within a pattern match in OCaml

How do I check the type of a variable within a pattern matching in OCaml, version I currently use is 4.13, that is available online.I found this post: OCaml: Type Checking ObjectsBut it doesnt really...

View Article

In Ocaml, how can I access the Lib module from the command line?

If I type ocaml at the command line and then run code involving e.g. the Lib.explode function, I get the error Error: Unbound module Lib. How can I fix this?

View Article


What is the safe and idiomatic way to check if two records (and two types in...

It seems like the codebase I'm working on complains if I'm using = or <> to check equality or inequality of two types that are not int types.I assume this was disabled so that I can't do the...

View Article

How to write sublist of a char list as [’1’; ’2’; ’3’; ’’; ’’; ’4’; ’5’; ’’;...

I am currently learning OCaml and i couldn't make sublist even thought I can make a list without spaces as ['1'; '2'; '3'; '4'; '5'; '6']. I couldn't find any sources to my question so thanks for...

View Article

Trouble computing recursive value of this function

let rec f (l: int list) : int * int = begin match l with | [] -> (0,0) | [x] (x,x) | x::y::tl -> let (a,b) = f tl in (x + a, y + b) endlet r = f [2;3;4;5;6]I'm thinking that the answer would...

View Article
Browsing all 518 articles
Browse latest View live