0% found this document useful (0 votes)
4 views

Message

The document contains definitions of several list processing functions in OCaml including length, nth, is_pos, get_max, init_list, append, put_list, init_board, is_board, print_board, get_cell, and put_cell.

Uploaded by

theo.charbonnier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Message

The document contains definitions of several list processing functions in OCaml including length, nth, is_pos, get_max, init_list, append, put_list, init_board, is_board, print_board, get_cell, and put_cell.

Uploaded by

theo.charbonnier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

let rec length list =

match list with


[] -> 0
|e::l -> 1+length l;;

let nth i list = if list = [] then


failwith "list is empty"
else if i<0 then
invalid_arg "nth: index must be a natural"
else let rec iem list i = match list with
[] -> failwith "nth: list is too
short"
|e::f when i=0 -> e
|e::f -> iem f (i-1)
in iem list i;;

let rec is_pos list = match list with


[] -> true
|e::f when e<0 -> false
|e::f -> is_pos f;;

let get_max list = if list=[] then


invalid_arg "get_max: empty list"
else let n=0 in let rec max list n=
match list with
[] -> n
|e::f when n<e -> max f e
|e::f -> max f n
in max list n;;

let rec init_list n x = if n=0 then


[]
else
x::init_list (n-1) x;;

let rec append list l = match list with


[] -> l
|e::f -> e::append f l;;
let rec put_list v i list = match list with
[] -> list
|e::f when i=0 -> v::f
|e::f -> e::put_list v (i-1) f;;

let init_board (l,c) v = let list = [] in let rec board list (l,c) v =
match (l,c) with
|(0,_) -> list
|_ -> board ((init_list c v)::list) (l-1,c) v
in board list (l,c) v;;

let is_board list = match list with


[] -> true
|e::f -> let n=length e in let rec board list n=
match list with
[] -> true
|e::f when n=length e -> board f n
|_ -> false
in board f n;;

let print_board list = let rec print_list list = match list with
[] -> ()
|e::f -> (
print_char(e);
print_list f;
)
in let rec print list=
match list with
[] -> ()
|e::f -> (
print_newline();
print_list(e);
print f
)
in print list;;

let rec get_cell (x,y) board =


if x<0 || y<0 then
invalid_arg "x or y is not positive"
else
match board with
[] -> invalid_arg "list is too short for x"
|e::f when x=0 -> let rec cell y e = match e with
[] -> invalid_arg "list too short for y"
|a::b when y=0 -> a
|a::b -> cell (y-1) b
in cell y e;
|e::f -> get_cell (x-1,y) f;;

let rec put_cell v (x,y) board =


if x<0 || y<0 then
invalid_arg "x or y is not positive"
else
match board with
[] -> board
|e::f when x=0 -> (put_list v y e)::f
|e::f -> e::put_cell v (x-1,y) f;;

You might also like