0% found this document useful (0 votes)
19 views4 pages

Ocaml

The document contains several recursive functions written in OCaml for list manipulation. These functions include 'compress' to remove consecutive duplicates, 'pack' to group duplicates, 'replicate' to repeat elements, 'drop' to remove every k-th element, and 'extract' to generate combinations of elements. Each function is defined with pattern matching and recursion to operate on lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Ocaml

The document contains several recursive functions written in OCaml for list manipulation. These functions include 'compress' to remove consecutive duplicates, 'pack' to group duplicates, 'replicate' to repeat elements, 'drop' to remove every k-th element, and 'extract' to generate combinations of elements. Each function is defined with pattern matching and recursion to operate on lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Onglet 1

let rec compress l = match l with


|[] -> []
|[a] -> [a]
|a::b::c ->
if a = b then compress(b::c) else a::(compress (b::c)) ;;

let rec pack l =


let rec aux k liste = match k,liste with
|_,[] -> []
|k,[c] -> [List.init k (fun i -> c)]
|_,c::d -> if List.hd(d)=c then aux (k+1) d else (List.init k (fun i -> c))::(pack d);
in aux 1 l;;

let rec replicate l k = match l with


|[] -> []
|a::b -> let rec aux i c liste = match i with
|0 -> liste
|j -> c::(aux (j-1) c liste)
in aux k a (replicate b k);;

let rec drop l k =


let rec aux liste j = match liste,j with
|[],_ -> []
|a::b,1 -> aux b k
|a::b,j -> a::(aux b (j-1));
in aux l k;;

let rec extract k l = match k,l with


|0,_ -> [[]]
|_,[] -> []
|1,[a] -> [[a]]
|k,(a::b) -> (extract k b)@(List.map (fun x -> a::x) (extract (k-1) b));;
Onglet 2

You might also like