在同一个函数中执行多个命令(Haskell)



我正试图在Haskell中的函数中执行一些语句,我在网上看了看,得到了一个想法,如果我用"做",我就可以做到。我用过它,但它仍然不适用于我,如果有人能看看并指导我做错了什么,我刚刚开始使用haskell,所以它在haskell语法方面有点困难。

我的功能:

type Rod = String
Move = (Integer, Rod, Rod)
hanoi :: Integer -> Rod -> Rod -> Rod -> [Move]
hanoi n source helper destination= if n==1 then [(n source destination)] else do
                                (hanoi (n-1) source helper destination) 
                                ([n source destination]) 
                                (hanoi (n-1) helper destination source)

我正在努力解决河内塔的问题。我想执行"做"之后的三个语句。如有任何帮助,我们将不胜感激。

提前感谢!!!

为了帮你一点忙,这里有一种方法可以让它编译并工作(几乎很好):

type Rod = String
type Move = (Integer, Rod, Rod)
hanoi :: Integer -> Rod -> Rod -> Rod -> [Move]
hanoi n source helper destination =
  if n==1 then
    [(n, source, destination)]
  else
    hanoi (n-1) source helper destination 
    ++ [(n, source, destination)]
    ++ hanoi (n-1) helper destination source

我改变的是:

  • Move提供了一个类型(我希望您想要一个元组)
  • 将结果更改为元组((n source destination) -> (n,source,destination)
  • 将结果与++连接

现在您只需要修复操作顺序的一个小问题;)它应该会打印出一个解决方案:D

最新更新