How to add increment counter in F #

I am trying to make the Hanoi Tower, but I do not know how to add a count counter. Here is my code:

open System

let disks = Int32.Parse(Console.ReadLine())

let rec hanoi num start finish =
  match num with
  | 0 -> [ ]
  | _ -> let temp = (6 - start - finish)
     (hanoi (num-1) start temp) @ [ start, finish ] @ (hanoi (num-1) temp finish)

[<EntryPoint>]
let main args =
  (hanoi disks 1 2) |> List.iter (fun pair -> match pair with
| a, b -> printf ": %A %A\n" a b)
  0

I'm trying to get him to print something like this

1: 1 3
2: 1 2
3: 3 2
etc...

I know that for

no formatting
1:
2:
3:

part. I know that formatting is correct

"%A: %A %A\n" *with some counter here* a b

however, I do not know how to do this. I searched on the Internet but found nothing. If anyone could help me, that would be very grateful.

Thank you in advance

+4
source share
1 answer

s952163 comment is the right answer here, but here are a bit more explanations to go with it.

List.iteri List.iter, , - .

hanoi disks 1 2 |> List.iteri (fun i (a, b) -> printfn "%d: %d %d" i a b)

. ,

  • hanoi - |> ,
  • printfn printf "...\n" - , . Windows "\ r\n" (, , )
  • - - , (a, b) . .
+6

Source: https://habr.com/ru/post/1660733/


All Articles