Search code examples
ocamltowers-of-hanoi

How do I change type int to 'a type? OCaml (Towers of Hanoi)


I am a newbie at OCaml. I wrote this code about the towers of Hanoi.

let rec hanoi (a,b,c) n =
    if n <> 0 then begin
        hanoi (a,c,b) (pred n);
        Printf.printf "%i %i\n" a b;
       hanoi (c,b,a) (pred n) 
  end;;

I print: a=origin tower and b=destination tower.

I would like to do this:

hanoi : 'a * 'a * 'a ­> int ­> ('a * 'a) list

How can i change int type to 'a type? Is there any way of writing 'a type? Should I use List.append to add a ('a list)?

Thanks.


Solution

  • Here is the function you really want :)

    let rec hanoi_list n (d,a,i) = match n with |0 -> [] |1 -> [ (d, a) ] |_ -> hanoi_list (n-1) (d,i,a) @ hanoi_list 1 (d,a,i) @ hanoi_list (n-1) (i,a,d);;

    Hope you will understand it :p