ocaml
An ML parser interpreter implemented in OCAML
This is a simple ML parser/interpreter implemented in OCAML. Below there are different versions.
Sorry for the comments in Italian. Please comment below if you don't get parts of the algorithm.
- fabio's blog
- Add new comment
- Read more
- 457 reads
OCAML Lazy Lists: Prime numbers
An algorithm implemented in OCAML to create a list of prime numbers using lazy lists.
Sorry for the code comments in Italian. Please add a comment below if you don't get parts of the algorithms.
(* Varesano Fabio - Liste LAZY *)
(* FUNZIONI PER L'USO DI LISTE LAZY *)
exception EmptyArg;;
(* Definizione del TIPO llist per le liste lazy *)
type 'a llist = Lnull| Lcons of (unit -> ('a * 'a llist));;
(* funzione che mi restituisce il PRIMO ELEMENTO della lista lazy
* lhd : 'a llist -> 'a =
*)
let lhd= function Lnull -> raise EmptyArg | Lcons(y) -> fst(y ());;
(* funzione che mi restituisce una lista lazy che contiene il resto della lista passata come argomento
* tranne il primo elemento
* ltl : 'a llist -> 'a llist =
*)
let ltl= function Lnull -> raise EmptyArg| Lcons(y) -> snd(y ());;
(* funzione che AGGIUNGE in testa un elemento ad una lista lazy *)
let lcons a ll = Lcons (function () ->(a, ll));;
(* funzione che definisce una lista lazy a partire da un intero n
* lfrom: int -> int llist
*)
let rec lfrom n = Lcons(function () -> (n,(lfrom (n+1))));;
(* funzione che prende i primi n elementi di una lista lazy passata come argomento e restituisce una lista lazy
* ltake: int -> 'a llist -> 'a llist
*)
let rec ltake (n, ll) = match (n,ll) with
(0,_) -> Lnull
|(n, Lnull) -> Lnull
|(n,Lcons(y)) -> let y1= y () in lcons (fst y1) (ltake(n-1, snd(y ())));;
(* funzione che trasforma una lista lazy in una lista
* llist2list : 'a llist -> 'a list
*)
let rec llist2list= function
Lnull -> []
| Lcons(y) -> let y1= y () in (fst y1)::llist2list (snd (y ()));;
(* FUNZIONI PER IL CALCOLO DEI NUMERI PRIMI *)
(* funzione che, data una lista, elimina i multipli di n, seguendo l'ordine di inserimento nella lista
* Il calcolo si ferma al raggiungimento di radice(n)
*)
let rec leliminaMultipli n= function
Lnull -> Lnull
|Lcons(y) -> let y1=y () in let first= fst y1 and tail = snd y1
in if first mod n = 0 then (leliminaMultipli n tail)
else Lcons(function () -> (first, leliminaMultipli n tail));;
(*funzione che effettua il CRIVELLO di eratostene per il calcolo dei numeri primi
*)
let rec lcrivello = function
Lnull -> Lnull
|Lcons(y) -> let y1=(y ()) in let first=fst y1
in Lcons(function() -> (first, lcrivello(leliminaMultipli first (snd y1))));;
(* funzione che restituisce la lista dei primi n NUMERI PRIMI calcolati col metodo di Eratostene*)
let primes = function
0 -> Lnull
| n -> ltake(n, lcrivello (lfrom 2));;
- fabio's blog
- Add new comment
- 786 reads
An OCAML implementation of the Odd/Even Quick Sort Algorithm
This is a simple implementation of the Odd/Even Quick Sort Algorithm in OCAML.
This algorithm orders even numbers before the odds numbers. Eg: given the numbers 6,3,1,2,4,5 it will return 2,4,6,1,3,5
(*
Fabio Varesano - 2007/05/11
Quick sort Odd-even
final order will be:
as example: 2,4,6,1,3,7
*)
let leq a b = if ( (a mod 2==0 && b mod 2==1) || ((a mod 2==0 && b mod 2==0) && a<=b) || ((a mod 2==1 && b mod 2==1) && a<=b)) then true else false;;
let rec quicksort leq list =
match list with
| [] -> []
| [x] -> [x]
| pivot::rest ->
let rec partition left right list =
match list with
| [] -> quicksort leq left @ (pivot :: quicksort leq right)
| head::tail -> if leq head pivot then partition (head::left) right tail
else partition left (head::right) tail
in partition [] [] rest;;
quicksort leq [3;4;6;1;9;20;5;7;4;2;8];;
- fabio's blog
- Add new comment
- 551 reads
A simple math expression calculator in ocaml
I'm studying for the course of "Programming Paradigm" (traslation of Paradigmi di Programmazione) where we study different programming approaches by using some new programming languages.
The first programming language we are using is ocaml, a functional programming oriented language (for more informations read the ocaml Wikipedia page).
As first exercise we have developed a simple math expression calculator: for example given a string like (3*2)+5 the program is able to calculate the result.
- fabio's blog
- Add new comment
- Read more
- 1862 reads

Recent comments
1 day 9 hours ago
2 days 1 hour ago
3 days 7 hours ago
4 days 14 hours ago
5 days 1 hour ago
5 days 3 hours ago
1 week 10 hours ago
1 week 23 hours ago
1 week 1 day ago
1 week 4 days ago