ocaml building module (super beginner)

I'm new to module programming using ocaml. I made a dictionary module such as module type DICT = sig type key type 'a dict ... val lookup : 'a dict -> key -> 'a option ... end module DictList : DICT with type key = string = struct type key = string

Execute the decoding of the length in OCaml

In my function below, lp is of form as in example [0,a;3,r;7,p;2,a]. The function run_length_decode takes lp and returns in above example [r;r;r;p;p;p;p;p;p;p;a;a]. let rec run_length_decode lp = match (List.hd lp) with [] ->[] | [0] -> rle_decode (

Calculate the days of a month

I am new to FP and OCaml.My question is how can I write a function in OCaml which will give me what day it is when entered an integer. I guess this is one way to do it but doesn't feel convenient and would need modifying for every month. Let's say th

Rewrite the schema code in OCaml?

(define unique (lambda (L) (cond ((null? L) L) ((null? (cdr L)) L) ((eqv? (car L) (car (cdr L))) (unique (cdr L))) (else (cons (car L) (unique (cdr L)))))) This above code(#racket language) finds the unique elements from the list. I want to translate

Syntax error in OCaml function

The following code gives an error: let alpha = Hashtbl.create 26 in let print_and_add a = print_char a; Hashtbl.add alpha a true;; let str = read_line () in String.iter (fun x -> if Hashtbl.mem alpha x=false then print_and_add x) str What it's suppos

Include the submodule of a functor application

I have this functor that has a submodule: module type PT = sig type t val to_string: t -> string end module A(P:PT) = struct module SubA = struct type t = T of P.t | F of float end end And I want to extend it using an include. This even works: module

OCaml Optional Argument

How can I write a function in OCaml in which one or more argument are optional? let foo x y z = if(x+y > z) then true else false;; If foo do not receive the z argument it uses 0 as z. foo 3 3 2 -> true foo 3 3 10 -> false foo 2 1 -> true Is th

Custom OCaml compiler warnings

Is it possible to make ocamlc produce custom warnings when compiling? I have some half-finished code, and I want to be reminded that it's not finished. As an example: I want to write a reverse function. I can't be bothered to work out how to code it

If the instructions end early if they are false in OCaml

Assuming pos_of_xy (x, y) n is already defined to return an int, this code segment exits once one of the if statements returns false as if they were nested. I need them all to run regardless of the previous if statements. I'm not sure what I am forge

Ocaml: why these types?

Hi could someone please explain why the types are as outlined below? I understand that they would have to be int because x+y+z but the number of arguments (ie ->) seems arbitrary to me. let f x y z = x+y+z in f 1 2 3 // int let f x y z = x+y+z in f 1

How can I implement interop between OCaml and C ++?

I want to create a bridge between OCaml and C++. For instance I want to use some constructions written in OCaml in C++. How can I achieve this? Are there any libraries, bindings for this?You should read the relevant part of the language manual: Inter

OCaml - Get clues about what the _Syntax_ error is

I'm starting to do write some OCaml but I find finding Syntax errors quite hard. I come from JavaScript where it would tell me Unexpected token ***. But here, all I get is Syntax error... Is there some way to get a more explicit message? Thank you in

How to learn OCaml effectively?

Ok, I am a Java programmer without any functional programming knowledge. Now I have been currently learning OCaml for 2 weeks, and I don't get even a clue about OCaml. There are several tutorials and books suggested here: https://stackoverflow.com/qu

Code Generation for Math Problems

I would like to write a program that takes in a description of a mathematical (optimization) problem, parses it, and generates compact, efficient C code that solves it. I have a hacked up solution to a much smaller, more specific problem, in python,

List.Fold_Left type system in Ocaml?

Write an Ocaml function list_print : string list -> unit that prints all the strings in a list from left to right: So Lets say I've got an Ocaml function list_print: string list -> unit that prints all the strings in a list from left to write. Now t

OCaml performance of exceptions

I've often read that exceptions are somewhat slow and should be avoided if performance is an issue (for instance, in Java, F#, etc). Does that apply to common OCaml functions such as Hashtbl.find, which return exceptions for elements not found? In pa

How to present big_int?

I am working with big_int type. I looked in the OCaml's library Pervasives. For example: in Int32 let t = 5l Printf.printf "%ld" t How can I define t and which %?d if I want to declare it is an big_int?Below is a toplevel session. The #load dire

string_dec and string in the Ocaml library

I have file: String0.ml extracted from String.v (It is from Coq library) String.ml : is a string library of Ocaml After extracted my test file from Coq to Ocaml, I want to used String.ml in the library of Ocaml, so I write an replace command to repla

List list with a specific length in OCaml

I'm wondering how can I write a function: 'a list*int -> 'a list*list that transforms a given list to a list of lists with given maximal length. For example: segments( [1;2;3;4;5;6;7;8;9], 2 ) => [ [1;2]; [3;4]; [5;6]; [7;8]; [9] ]Here is a tail-rec

How to manage a list of polymorphic variants?

Let two variant types : type typeA = | A1 | A2 ;; type typeB = | B1 of typeA | B2 of typeA ;; and type-checking functions : let isA1 = function A1 -> true | _ -> false;; let isA2 = function A2 -> true | _ -> false;; let isB1 = function B1 e -&

Abuse algebra of algebraic data types - why does it work?

The 'algebraic' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand X² f

OCaml: List mapping a function with 2 entries

I have a function sqrt which takes 2 floating point values, tolerance and number and gives out square root of the number within the specified tolerance. I use approximation method to do it. let rec sqrt_rec approx tol number = ..................;; le

OCaml MicroBenchmark

I am trying a basic microbenchmark comparison of c with ocaml. I have heard that for the fibonacci program, c and ocaml are about the same, but I can't replicate those results. I compile the c code with gcc -O3 fib.c -o c-code, and compile the OCaml

Mutable data in OCaml

I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error, Here is my code type vector = {a:float;b:float};; type vec_store = {mutable seq:vector array;mutable size:int};; let max_seq_length = ref 200;; ex