programming_pictoral
programming_pictoral

Reputation: 19

Reverse Mutable List Ocaml

I need to write a function that takes two mutable lists as input and as an output has the first list reversed appended to the second list. The type definition is:

type 'a mylist = 'a listcell ref
  and 'a listcell = Nil | Cons of 'a * ('a mylist)

I am aware how to reverse a regular list but am having some confusion on writing a function that does this by destructively changing the first list. Here is what I have but I keep getting a type error:

let rec rev_app l1 l2 =
  let rec rev_app' l3 l4 =
    match !l1 with
    | Nil -> l2
    | Cons (x,t) -> ref (rev_app t (Cons(x,l4)))
  in rev_app' l1 l2

Upvotes: 0

Views: 428

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

Your inner call to is to rev_app, not to rev_app'. So your code is somewhat similar to saying:

let rec f x = ref (f x)

As a side comment, it is good manners to quote the actual error you're getting. Just saying "a type error" isn't so helpful.

Upvotes: 1

Related Questions