Questions tagged [ocaml]

OCaml is a strict statically-typed functional programming language, focusing on expressivity, correctness and efficiency.

Filter by
Sorted by
Tagged with
1
vote
0answers
34 views

Longest Substring Without Repeating Characters in ReasonML/Ocaml

LeetCode Problems 3. Longest substring without repeating characters Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", ...
2
votes
0answers
29 views

Project Euler Problem 54 poker hands in OCaml

I've been refactoring my OCaml solution for Project Euler problem 54, which is about comparing poker hands. While I'm mostly satisfied with my second iteration of the refactor, I stumbled upon two ...
1
vote
0answers
33 views

Scrolling text (marquee) in terminal

I wrote an OCaml program that displays a scrolling marquee in the terminal: Code: ...
2
votes
1answer
41 views

Sum of two elements of a list equaling a target

In effort to learn OCaml, I'm going through some easy LeetCode problems and attempting to implement them. I arrive at the correct answer, but I'm still developing an intuition for canonical OCaml code....
1
vote
0answers
134 views

OCaml backend for REST API and JSON data

I want to create a backend for a simple REST API in OCaml. For this I use the libs httpaf and yojson. I have something that works but I would like some reviews. Here is the structure of my little ...
2
votes
0answers
45 views

Interleave two lists, preserving the order of elements in their original list

I need to write an OCaml program that return all possible zip of two lists together while preserving the order of the element in each list. For example, if we zip the lists [1;2] and [3;4], we get [[1;...
1
vote
1answer
92 views

Advent of Code 2018 Day 1 in OCaml: Find length of cycles when accumulating deltas

I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same ...
2
votes
1answer
141 views

Detect single-character xor (OCaml)

I have been working through the Cryptopals challenges in Ocaml. My interest in Ocaml is to better learn functional programming techniques and effective usage of the type system. Set 1 Challenge 4 ...
4
votes
1answer
1k views

Deleting a node from Binary Tree in OCaml

I am currently exploring OCaml and wrote the following implementation of deleting a node from a binary tree ...
6
votes
0answers
177 views

OCaml safe uninitialized array

One of the annoying things about OCaml arrays is that every cell has to be initialized when the array is first created. I'm trying to implement an array in OCaml that distinguishes ...
4
votes
1answer
269 views

Subtract ordered lists

Newbie OCaml review: Given lists xs and ys that are both ordered in the same way (e.g. monotonically ordered integers), I want ...
3
votes
1answer
59 views

Function that calls a function with the terminal temporarily set to raw mode

I'm working through Build Your Own Text Editor, a walkthrough of the code to an extremely small text editor for a posix terminal in C, but I'm attempting to follow along in-the-spirit with OCaml. I'...
1
vote
1answer
132 views

Concurrent version of List.map in Ocaml

I would like to write a pseudo version of List.map with the help of the Event and Thread ...
5
votes
1answer
308 views

Insert function for a trie in Ocaml

I don't know how could I write smarter/more elegant code for the add function , it just seems so meaty and hard to read. This trie only stores words and marks their ...
1
vote
0answers
114 views

Implementation of a KNN in OCaml

I wrote the following implementation of the k-nearest neighbor algorithm (for a binary classification task). I am not familiar with OCaml's built in functions, I have the feeling that some of them ...
4
votes
1answer
137 views

Pattern matching a ZMachine

Anyone following Eric Lippert's blog will know that he has an on going series where he is building a ZMachine in OCaml. C# 7 is another step forward in adding features normally found in functional ...
2
votes
1answer
62 views

Making the basic accumulator in Real World OCaml use Async

I was going through Real World OCaml and wanted to make the program in "A Complete Program" use Async. It works now, but I'm wondering how idiomatic it is, and if I'...
6
votes
1answer
640 views

Client Server code in OCaml style

I have a piece of code that works but, as I start learning OCaml, I am not confortable with the "functionnal style" to write code. The following code create two threads. One for a tcp server that ...
1
vote
1answer
131 views

Streaming learning OCaml

I wrote a simple online logistic regression, calibrated using gradient descent to compare the speed of an OCaml implementation vs the same Python script, executed with Pypy. It turned out that the ...
1
vote
1answer
229 views

Optimize hash OCaml

I am running a simple OCaml program which opens a CSV file with a pseudo dict-reader and hashes "key" + "value" (where key and values are strings). Then some counts are evaluated on the hashes (but it ...
5
votes
1answer
255 views

Syracuse conjecture in OCaml

I am wondering how to optimize the following OCaml simple code. ...
2
votes
1answer
123 views

L-Systems in OCaml

For reference: L-system I'm mostly looking for general style stuff here and in particular: if my type parameters are too crazy — I wanted two parameters so that variables and constants could have ...
3
votes
3answers
112 views

OCaml rev_int function

With my original code I kept getting Error: Parse error: [expr level ;] expected after "in" (in [expr]) on the line ...
2
votes
0answers
61 views

Decompose string into parenthesed blocks in OCaml

The purpose of the OCaml code below is to decompose (or "parse") a string into parenthesed (or possibly non-parenthesed) blocks. ...
3
votes
1answer
1k views

Recursive bubble sort in OCaml

I'm new in OCaml and just want to be sure that I write code in "ocaml way". My other first programs in OCaml was imperative and looks like python-code. So I decided to write simple code and share it ...
4
votes
1answer
1k views

Generating permutations in OCaml

...
2
votes
1answer
77 views

Find zeroes in a matrix

I am looking for the elements that are zeroes in a square matrix (of size n), as a list of pair. I don't really like the following code, some condition seems ...
3
votes
1answer
333 views

Finite state machine written in OCaml to test for the sequence 0,1

I got inspired by Stack Overflow to build a simple state machine which can test if a sequence contains 0,1. I have two questions: How can I get rid of line ...
4
votes
2answers
588 views

Snake Game OCaml

I wrote the following two players snake game in OCaml. It is working fine (I did not pay much efforts to the graphical appearance though). As a beginner in OCaml, I would be happy to have some review. ...
3
votes
1answer
1k views

String replacement in OCaml

This is part of my first OCaml program. Its job is to replace a set of placeholder characters with umlauts. Taking the German word Ruebe as an example, the program ...
11
votes
1answer
229 views

Reference Implementation for Honey Badger programming language

I've been working on my own dynamically-typed, dynamically-scoped, imperative programming language called Honey Badger and feel that it's reached a stable enough state that I want someone else's eyes ...
2
votes
1answer
136 views

Print last Sunday of each month in a given year

I'm an experienced programmer trying to learn OCaml. I figured a good way to do that would be to pick a simple task on Rosetta code that didn't yet have an OCaml example and do it myself. With that in ...
2
votes
1answer
784 views

Infix epression evaluation

I'm looking for some feedback on my OCaml implementation of some methods to translate infix expressions to postfix and then evaluate them. I'm very new to OCaml, coming from C#/Java and JavaScript ...
2
votes
1answer
538 views

Comparing run-length decoding algorithms

I'm working on Ocaml.org 99 problems, and I solved the run-length decode one. Here is the solution given by the site: ...
8
votes
1answer
568 views

Combinations of size k from a list in OCaml

I wanted to write a small but non-trivial function in OCaml to test my understanding. I have only a basic understanding of the libraries, and no idea at all of what is considered good style. The ...
2
votes
2answers
179 views

Caml light and finding the prime factors

I wrote the following Caml light code to find prime factors of a number (part of an early Project Euler question). Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. ...
6
votes
1answer
268 views

I've written remainder(x,y) in OCaml. Is there more efficient than O(n)?

Here's my code: let rem x y = let rec aux acc i n = if i=n then acc else if acc+1=y then aux 0 (i+1) n else aux (acc+1) (i+1) n in aux 0 0 x;; I'...
2
votes
1answer
822 views

Functional Programming: int to bignum conversions

I'm doing an exercise where I need to convert int to bignum in OCaml, for example: ...
2
votes
1answer
1k views

Ocaml Functional Programming Tree

I'm writing a function called unzip that turns: [(1,2);(3,4);(5,6)] to ([1;3;5],[2;4;6]) I've got a working implementation ...
3
votes
3answers
328 views

Project Euler 10 - Summation of primes

I'm currently attempting to learn OCaml, and I'm working thought the Project Euler problems to do so. Here's some code I knocked together for problem 10. I am looking for idiomatic feedback rather ...
3
votes
3answers
9k views

Is this use of (let … and …) in OCaml poor style?

In other languages, I prefer to arrange source files so that simpler and more widely useful concepts are introduced before implementation details, and I try where possible to make complex ...
3
votes
1answer
772 views

Is this binary search close to idiomatic OCaml?

...
5
votes
2answers
1k views

String Trie in OCaml

I'm pretty new to OCaml, so any feedback is appreciated. The goal is to implement a trie that efficiently stores and sorts strings. ...
15
votes
0answers
162 views

Timeoutable computations module

Defines a simple module for timeoutable computations, with the ability to return arbitrary intermediary results on timeout or the final value otherwise. It also allows default return values. The ...