Quantcast
Browsing all 527 articles
Browse latest View live

OCaml combine fprintf and sprintf

I want to write a function that can either print something to a string or to an out_channel. I'm stuck with the internal format stuff however.My approach is something like this:type pr_target = Channel...

View Article


Intellij IDEA sdk setup for ocaml

I am trying to setup sdk for ocaml . I got all opam and ocaml installed and works fine in cli.But when I try to add sdk to the ocaml installtion...

View Article


Tail recursive List.map

The typical List.map function in OCaml is pretty simple, it takes a function and a list, and applies the function to each items of the list recursively. I now need to convert List.map into a tail...

View Article

Base causes issue with tuple destructuring

When Base is added to the following code, why does the OCaml compiler expect h and w to be ints? tup is supposed to be a tuple - is there a syntactic issue? What is it about Base that causes this...

View Article

Combine two lists while removing duplicates

The goal is to combine (append) two lists while removing the duplicates.let rec find_dup a lst = match lst with | [] -> false | hd::tl -> if (hd == a) then true else find_dup a tl;;let rec app...

View Article


OCaml type error (Variants/Pattern Matching)

Trying to do a simple pattern matching with variants that will give me the in order concatenation of binary tree node strings, but getting an error in the recursive implementation:type btnode = | Leaf...

View Article

Is there a way to `match` on a string that starts with some value? [duplicate]

For example, consider the following expression:match string with| "Foo " ^ rest -> rest| "Bar " ^ rest -> rest| _ -> "unmatched"Unfortunately, this is a syntax error. Is there some way to...

View Article

How to handle exception correctly

I am new to OCaml language. I wrote the function for studying numbers multiplication. But I faced with problem in case of exception handling. The deal is when int_of_string function can't convert the...

View Article


How do dune wrapped libraries with explicit module interfaces affect build...

I'm working in a monorepo with a lot of OCaml libraries that are (wrapped false) (see dune stanza reference)Is using dune wrapped libraries with explicit module interfaces likely to reduce link...

View Article


Image may be NSFW.
Clik here to view.

Pascal Triangle in OCaml: Get next line as a list

I am trying to get an OCaml function which gives the next line of a Pascal's triangle as a list for example:(next_line: int -> list -> int list)let get_next_line [1;1] returns [1;2;1] and so on.I...

View Article

Getting the two minimum values from a list in OCaml

I need to write a function twoMin: int list → int * int using foldl that returns the two smallest values (not elements) of a list. You may assume that the list contains at least two elements with...

View Article

Append a list on to another in oCaml

I need a tail recursive function that appends a list1 infront of list2.I tried it this waylet rec append lst1 lst2 acc = match lst1 with | [] -> acc @ lst2 | hd::tl -> append lst1 tl...

View Article

Best practices for chain of validations

I am building a Rest API using Sihl OCaml framework, so I need to parse incoming HTTP request, validating JSON body.I want to use some reusable functions with a "readable" style, but I am having issues...

View Article


Ocaml types behaving weirldy - eta reduction produces a type error [duplicate]

So, i'm just training in the art of ocaml, trying to write a little parsing combinator library. In development i ran into this pretty funny thing. can you please explain why does the ocaml compiler do...

View Article

Image may be NSFW.
Clik here to view.

ocaml-lsp-server giving "unbound module" errors

I am developing an Ocaml project using WSL and VSCode with the Ocaml Platform extension running ocaml-lsp-server. My project builds and runs correctly, but in VSCode there are a bunch of red squiggly...

View Article


How to write fold_left in CPS?

Even though it's already tail-recursive, It's still interesting to see a CPS version of it.Here's the standart fold_left:let rec myFoldLeft f acc list = match list with | [] -> acc | h :: tl ->...

View Article

Submatrix Ocaml

let moja_matrika1 = [[1; 2]; [3; 4]];;let moja_matrika2 = [[4; 7; 8]; [3; 2; 1]];;let rec does_it_contain (lis1, lis2) =if (List.hd lis1 = []) then falseelse if (List.hd lis1 = lis2) then trueelse...

View Article


Figuring out why these types differ when using a catch all in the pattern...

I'd like some help with understanding how type inference affects the following code, which comes from a previous question (which after some thoughts I've simplified to the following):type 'a result = |...

View Article

How to set locale in ocaml?

I'm interested in changing LC_ALL in an ocaml program. The equivalent C function would be setlocale(LC_ALL, "my_new_locale");. How would one do this in ocaml?

View Article

Confused about how to change the type of a function

I am writing a small parser combinator library, currently I am implementing a mapping function that's able to change the contents of the result of a successfully applied combinatorlet pmap mapper p =...

View Article
Browsing all 527 articles
Browse latest View live


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