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

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


Library ounit2 not found

I want to run unit test with OCaml and OUnit2I have followed the instructions in this tutorial https://cs3110.github.io/textbook/chapters/data/ounit.htmlHowever, when I rundune buildI get the error$...

View Article


Name binding in pattern matching in OCaml

What does the name binding with the as- keyword do in this exercise:https://ocaml.org/problems#17# let split list n = let rec aux i acc = function | [] -> List.rev acc, [] | h :: t as l -> if i =...

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

What is Some in OCaml

This Ocaml code traverses a list and outputs the last element.I dont understand the second condition where we output Some xlet rec last = function | [] -> None | x::[] -> Some x| _ :: t ->...

View Article


assert (!isnan(x)) succeeds but printf("%f",x) shows -nan

I am using gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0.Ubuntu 24.04I have a bug in my code where a numerical analysis function is giving very different answers.Each of the following returns true. x is a...

View Article

Create directory when it does not exist

I want to create a new directory if it doesn't exist already.Tried several things, sadly I didn't find something in the Sys Library.The nearest solution I got was:if not (Sys.is_directory "vegetables")...

View Article

How to randomly group elements of two lists and make new list of the tuples...

If I have 2 lists ['a'; 'b'] and [1;2;3;4] I want to get something like [('a',2);('a',4);('b',3); ('b', 1)]. So the point is that I need to use all elements of second list once and I can use elements...

View Article


Why are dependently typed languages often not Turing complete? [closed]

For example, Agda and Coq.Compared with say, Idris2 and Fstar.Also, are there practical disadvantages to dependently typed programming languages such as Agda not being Turing complete, or is it in a...

View Article


What is wrong with this code for creating lists in OCaml?

I am trying to create a program in OCaml [randlist len max], which would generate an int list of length len with integers smaller than max.I am wondering what is wrong with the following code:let...

View Article

For loops in ocaml

I want to do something like the following.let switchgraph cases = let g = Graph.makeGraph() in let g = (Graph.addNode g 1) in for i = 2 to cases do let g = (Graph.addNode g i) in done gBut apparently,...

View Article

Can a dependently typed language be Turing complete? [closed]

It seems as though dependently typed languages are often not Turing complete. Why can we not allow every function to have general recursion (which would make the language Turing complete), instead of...

View Article

Include Non-OCaml/Reason Files in Dune Build?

I'm new to OCaml and Dune, and I'm trying to set up a basic project with test cases. I want my test module to be able to read in text files from a subdirectory and process the input of those files....

View Article


How to combine elements of a list into one element in Ocaml?

I am trying to combine a list of integers into one integer in Ocaml.For example,Input - List = [2,3,6,7,9]Desired Output - 97632.I know how to iterate a list but what operation/function should I be...

View Article

Install OCaml with graphics on mac with homebrew 2.0.0 (-with-x11 no longer...

Is there an alternative way to install the graphics module for use with ocaml on mac?Previously, I had used brew install ocaml --with-x11This no longer works, as the package option is no longer...

View Article


OCaml - append all elements of a list to all elements of another list

Given string lists [a, b, c] and [d, e] for example,return the list [ad, bd, cd, ae, be, ce].Please don't give me an answer, just a point the the right direction for a new learner.

View Article

split function: best practice [closed]

I have some experience with FP and I found this function online which splits a list in two halves:let split l = let n = List.length l in let rec splitAt k l = match k, l with | 0, _ -> [], l | _, []...

View Article


append all elements of a list to all elements of another list

Given string lists [a, b, c] and [d, e] for example,return the list [ad, bd, cd, ae, be, ce].Please don't give me an answer, just a point the the right direction for a new learner.

View Article

Iterating over split string in OCaml

Let's say I have a string:"ab bc cdv gfed aqb ahf sdabcdefI want toa) Split it by '' and/or '\r\n', '\t'b) Iterate over newly created list of these substrings, split by separators and match each of...

View Article

Insert function for a matrix in OCaml is not working

class virtual ['a] matrix_game m pf = object val matrix : 'a array array = m val rows = Array.length m val cols = Array.length m.(0) val print_function : 'a -> unit = pf method print = for i = (-1)...

View Article
Browsing all 518 articles
Browse latest View live


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