简体   繁体   English

例外:Prelude.last:Haskell解决8皇后的空列表?

[英]Exception: Prelude.last: empty list in Haskell solving 8-queens?

I'm solving the 8-queens problem in Haskell using only basic functions nothing fancy 我正在仅使用基本功能来解决Haskell中的8皇后问题
this is the code: 这是代码:

queens = [[x1,x2,x3,x4,x5,x6,x7,x8]|x1<-[1..8],x2<-[1..8],x3<-[1..8],x4<-[1..8],x5<-[1..8],x6<-[1..8],x7<-[1..8],x8<-[1..8],safeH [x2,x3,x4,x5,x6,x7,x8] x1,safeD [x2,x3,x4,x5,x6,x7,x8] x1 [x1,x2,x3,x4,x5,x6,x7,x8] 1] 
safeH l e = if elem e l then False 
            else if length (l)/=0 then safeH(tail l)(head l) 
                    else True
safeD l e xs n = if last(xs)/=e || length xs == 0 then
                if length(l)/=0 then 
                    if (head(l)+n==e || head(l)-n==e) then False 
                    else safeD(tail l)(e)(xs)(n+1) 
                else safeD(tail xs)(head xs)(tail xs)(1)
            else True

To clarify the SafeH function checks that no queens are in the same row H stands for Horizantly while the SafeD is supposed to check for diagonal conflicts 为了澄清SafeH函数,检查是否在同一行中没有皇后字母H代表Horrizant,而SafeD应该检查对角线冲突
I am sure that the SafeH function is okay the problem with SafeD 我确定SafeH功能可以解决SafeD的问题
and when compiling the code it gives me no problems but when calling the queens function 当编译代码时,它没有问题,但是调用queens函数时
it gives me this error: 它给了我这个错误:

[1 of 1] Compiling Main             ( y.hs, interpreted )
Ok, modules loaded: Main.
*Main> queens
*** Exception: Prelude.last: empty list

can anyone please help me?? 谁能帮帮我吗?? thanks in advance for every thing :) 预先感谢每一件事:)

You can fix the immediate problem by checking the length of xs before calling last : 您可以通过在调用last之前检查xs的长度来解决紧迫的问题:

safeD l e xs n = if length xs == 0 || last(xs)/=e then ...

However, you will then run into another problem because you call safeD(tail xs)(head xs)(tail xs)(1) inside the then part of this branch, and you can reach the then part of this branch when length xs == 0 . 但是,将然后运行到另一个问题,因为你打电话safeD(tail xs)(head xs)(tail xs)(1)的内部then该分支的一部分,并且可以达到then该分支的一部分,当length xs == 0

I strongly recommend learning a little bit about pattern matching ( Gentle Intro section , Haskell Report section ) and trying to write this entire code snippet without ever calling head , tail , init , last , or length . 我强烈建议您学习一些有关模式匹配的知识(“ 温和介绍”部分 ,“ Haskell报告”部分 ),并尝试编写整个代码片段,而无需调用headtailinitlastlength Instead, use the two patterns [] for matching empty lists and (x:xs) (or similar) for matching lists that start as x and end with xs ; 取而代之的是,使用两种模式[]匹配空列表,使用(x:xs) (或类似模式)匹配以x开头并以xs结尾的列表; if necessary, a call to reverse once in a while would be okay. 如果必要的话,调用reverse在偶尔会好起来的。

Good luck, and let us know how you fare and where you get stuck! 祝您好运,并让我们知道您的票价和卡住的地方!

Ever thought of solving this problem for zero queens first? 有没有想过先解决零皇后的问题?

Then for one queen? 那一个女王呢?

Then spot the inductive / recursive pattern?.. I am not quoting the 3-liner solution, as it looks like your homework. 然后找出归纳/递归模式吗?我没有引用3线性解决方案,因为它看起来像您的作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM