I found this type zipper
and insert
functions defined like this:
type 'a zipper = 'a list * int;;
exception Empty
let empty = ([], 0)
let insert ((l, n): 'a zipper) (a: 'a) : 'a zipper =
let rec ins ll nn =
if nn = 0 then a::ll
else
match ll with
| [] -> raise Empty
| h::q -> h::(ins q (nn-1))
in
(ins l n, n)
How can I convert this function to use only one let
and without that in (ins l n, n)
Reducing the number of local function definitions is mostly a non-sensical metric, but you can certainly do it
let cons x (l,n) = (x::l,n+1)
let rec insert (l, n) a =
if n = 0 then a::l,n
else
match l with
| [] -> raise Empty
| h::q -> cons h (insert (q,n-1) a)