Questions tagged [ocaml]

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

Filter by
Sorted by
Tagged with
2
votes
0answers
26 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;...
0
votes
0answers
27 views

ocaml merge sort

How do I make this more "ocamlic"? ...
1
vote
1answer
76 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
93 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 ...
2
votes
1answer
685 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
146 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
187 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
55 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
113 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
277 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
107 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
135 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
54 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
597 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
121 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
165 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
208 views

Syracuse conjecture in OCaml

I am wondering how to optimize the following OCaml simple code. ...
2
votes
1answer
112 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
104 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
56 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
945 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
836 views

Generating permutations in OCaml

...
2
votes
1answer
74 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
267 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
1answer
461 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
217 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
128 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
714 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
486 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: ...
7
votes
1answer
493 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
163 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
250 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
738 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
313 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
8k 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
761 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
155 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 ...