【发布时间】:2017-03-28 16:21:09
【问题描述】:
我正在尝试制作河内塔,但我不知道如何添加计数增量器。这是我的代码:
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
我正试图让它打印出这样的东西
1: 1 3
2: 1 2
3: 3 2
etc...
我知道没有设置格式
1:
2:
3:
部分。我知道正确的格式是
"%A: %A %A\n" *with some counter here* a b
但是我不知道该怎么做。我已经在网上寻找答案,但我没有找到任何东西。如果有人可以帮助我,将不胜感激。
提前谢谢你
【问题讨论】:
-
List.iteri 会给你柜台。
-
这与您的问题无关,但您可能想在学习语言时查看使用
.fsx文件和F# interactive - 您根本不必编译代码,您可以在同一窗口中调整并查看反馈。您可以通过突出显示并按alt-enter将 Visual Studio 中的代码发送到 F# 交互。
标签: f# counter increment towers-of-hanoi