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

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

How can I quickly split an utf8 string into chars in OCaml?

I'm handling a string with special characters and I want to split it into Unicode characters, but apparently OCaml strings are encoded in utf-8 while OCaml chars are 1-byte values, and it appears that...

View Article


Filtering an array in OCaml

I have an array of integers where I'm treating 0 in a special way. I need to manipulate the array so that the output is an array of the same size with all the zeros after all the non-zero values. For...

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

Weird function on trees Ocaml

A Node is called beautiful if it's value is greater than the value of any other node, which can be found on the way to the root. The problem is to count the beautiful nodes on a given tree.Here is...

View Article

Chaining clauses in a pattern match, how to add missing bindings

I have a type defined as such:type quantity = Value of int | Direction of int * int | Vector of int * int * intLater I have functions that operate on values of the latter two subtypes together:match q...

View Article


Convert each string in a list into a list of strings

I have two functions, one islet make_row delim str = List.map String.trim (Str.split (Str.regexp delim) str)and the other islet rec table_of_stringlist delim rlist = match rlist with| h::[]->...

View Article


Unbound constructor Why3.Number.ConstReal

I'm trying to run a code which has been built with old versions of why3 (0.99.1). Now, I'm trying to run it with the latest version of why3 and encountered some errors.This is the error that I...

View Article

Iterate through nested list data type in OCaml

I'm trying to iterate through the Sexp.t data type, defined in the Jane Street Sexplib module. I want to print out the data type in a way that shows its recursive structure for debugging. So far, this...

View Article

OCaml mod function returns different result compared with %

The modulo function in OCamlmod return results different when compared with the modulo operator in python.OCaml:# -1 mod 4- : int = -1Python:>>> -1 % 43Why are the result different?.Is there...

View Article


Use Set.Make.iter to transform the elements of a set?

I've got a library which implements a set (interface with documentation available here: http://pastebin.com/j9QUyN1G). I understand everything apart from this fragment:val iter : ('a -> unit) ->...

View Article

Collecting the output of an external command using OCaml

What is the right way to call an external command and collect its output in OCaml?In Python, I can do something like this:os.popen('cmd').read()How I can get all of an external program's output in...

View Article

Split list into list of lists with a list as a parameter for where to split...

I am new to OCaml and curious as to how to write a function called Seperate_by that takes in two parameters, a list and a list of elements on where to split the original list.For example,Seperate_by...

View Article

Unable to use Core on Windows

I'm trying to use the Core package in OCaml, and I cannot seem to get it working. I have installed the core package using opam install core, which worked just fine. However, when I try to load the...

View Article



Ocaml using Core lib in windows

Having some trouble using [Core] library in windows.For 5.x.x I'm getting the following error with open Core:File "bin/dune", line 3, characters 7-11:3 | (name main) ^^^^** Cannot resolve symbols for...

View Article
Browsing latest articles
Browse All 531 View Live


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