本文介绍了河内移动func。在Haskell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 类型Position = Int 类型Move =(Position,Position )类型Towers =([Int],[Int],[Int]) hanoi 1 ij = [(i,j)] hanoi nij = hanoi n' i otherT ++ [(i,j)] ++ hanoi n'otherT j 其中n'= n-1 otherT = 1 + 2 + 3-ij - 其他塔 move ::([Move],Towers) - > ([Move],Towers) move([],(xs,ys,zs))=((errorError),(xs,ys,zs)) move((( ,b):tail),(xs,ys,zs))| a> 3 =(tail,((errorError),ys,zs)) | b> 3 =(tail,((errorError),ys,zs)) |否则= hilfsfunktion(((a,b):tail),(xs,ys,zs)) hilfsfunktion(((1,2):tail),((x:xs),( y:ys),zs)) | x | x> y =(tail,(xs,(errortoo big),(errortoo big))) hilfsfunktion(((1,2):tail),((x:xs ),(),(),zs))=(tail,(xs,x:[],zs)) $ b $ pre $ all_moves ::([Move],Towers) - > Towers all_moves([],(xs,ys,zs))=(xs,ys,zs) all_moves movetowers = all_moves(move movetowers) 但我无法在haskell中工作。 *主> move(it,([1,2,3],[],[])) ***例外:Tu.hs:(19,1) - (22,72):非穷举模式功能hilfsfunktion 我的问题: 这是否需要更多更正? (a,b):tail li> 解决方案首先,你陷入了新手Haskellers的常见错误;认为所有函数参数都应该放在括号内,如下所示: foo(a,b,c)= .... 在大多数语言中,这将是一个三参数函数。在Haskell中,它的一个函数采用由三元组元组成的单个参数。你应该写出 foo abc = .... 这样可以避免多余的括号,并且在开始使用高阶函数时会让生活变得更加简单。 现在开始你的问题的肉: 你的hilfsfunktion定义了两种情况。在这两种情况下,参数中的第一个列表必须以元素(1,2)开始。如果你传递了其他任何内容,它将无法匹配任何一种情况,并因此导致获得异常消息。 I saw one answer in another post:type Position = Inttype Move = (Position,Position) type Towers = ([Int],[Int],[Int])hanoi 1 i j = [(i,j)] hanoi n i j = hanoi n' i otherT ++ [(i,j)] ++ hanoi n' otherT j where n' = n-1 otherT = 1+2+3-i-j -- other towermove ::([Move],Towers) -> ([Move],Towers) move ([],(xs,ys,zs) ) = ((error "Error"),(xs,ys,zs) ) move (((a,b): tail), (xs,ys,zs) ) | a > 3 = (tail, ((error "Error"),ys,zs) ) | b > 3 = (tail, ((error "Error"),ys,zs ) ) | otherwise = hilfsfunktion (((a,b): tail), (xs,ys,zs) )hilfsfunktion (((1,2): tail), ((x:xs),(y:ys),zs) ) | x < y = (tail, (xs, (x:y:ys),zs) ) | x > y = (tail, (xs, (error "too big"),(error "too big"))) hilfsfunktion (((1,2): tail), ((x:xs), [],zs) ) = (tail, (xs, x:[],zs) )corrected one :all_moves :: ([Move], Towers) -> Towersall_moves ([], (xs, ys, zs)) = (xs, ys, zs)all_moves movetowers = all_moves (move movetowers)But I can't make it work in haskell.*Main> move(it, ([1,2,3],[],[])) *** Exception: Tu.hs:(19,1)-(22,72): Non-exhaustive patterns in function hilfsfunktionMy questions:Does this need more corrections?How does (a,b):tail work here? 解决方案 First thing is, you have fallen into a common mistake of novice Haskellers; thinking that all function arguments should be in brackets, like this:foo (a, b, c) = ....In most languages this would be a three-argument function. In Haskell its a function that takes a single argument consisting of a three-element tuple. You ought to writefoo a b c = ....This avoids extraneous brackets and makes life much easier when you start using higher order functions.Now on to the meat of your question:Your hilfsfunktion defines two cases. In both of those cases the first list in the argument has to start with the element (1,2). If you pass anything else in then it will fail to match either of the cases, and hence causes the exception message you got. 这篇关于河内移动func。在Haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 16:36