0

基本的な知識だけで、高度な関数を使用せずに Haskell で 8 クイーンの問題を解決しようとしています。ここまでしか来ていませんが、理解できないエラーが発生しています。コード:

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]
safeH xs e = if length xs == 1 then head xs 
                 else e /= safeH (tail xs) (head xs)

エラーメッセージは次のとおりです。

y.hs:1:42:
    No instance for (Num Bool) arising from the literal `1'
    Possible fix: add an instance declaration for (Num Bool)
    In the expression: 1
    In the expression: [1 .. 8]
    In a stmt of a list comprehension: x1 <- [1 .. 8]
[1 of 1] Compiling Main             ( y.hs, interpreted )
Failed, modules loaded: none.
4

2 に答える 2

5

犯人は

                      .........
                      safeH [x2,x3,x4,x5,x6,x7,x8] x1]
safeH xs e = if length xs == 1 then head xs 
                 else e /= safeH (tail xs) (head xs)

具体的には、

                 else e /= safeH (tail xs) (head xs)

なぜならe == x1。したがって、一方では、リスト内包表記のテストとして使用される をsafeH返します。BoolOTOH とその結果を比較しますx1。これは1、とりわけ ( x1<-[1..8]) です。すなわちNum1。これも である必要がありますBool。したがって、エラー。


1のような数値リテラル1は、多相型の値として解析されますNum a => a。つまり、その具象型はNum型クラスに属している必要があります。具象型もBoolここにあると判断されるため、このコードが型チェックを行うには、型クラスにBool属している必要があります。Numしたがって、instance for (Num Bool)が求められます。

于 2013-08-19T19:47:15.243 に答える