无法匹配预期类型`((Int,Int,Int) - > Int,b5)'

时间:2015-06-07 12:06:07

标签: haskell

不知道为什么调试多次仍然有错误,即使显式声明类型 int,不知道b0,a0,b2来自哪里

 ghc -o hello main.hs
[1 of 1] Compiling Main             ( main.hs, main.o )

main.hs:92:10:
    Couldn't match expected type `(Int, b0)'
                with actual type `(Int, Int, Int)'
    In the first argument of `fst', namely `x'
    In the first argument of `(==)', namely `fst x'
    In the first argument of `(&&)', namely `fst x == a1'

main.hs:92:25:
    Couldn't match expected type `((Int, Int, Int) -> Int, b1)'
                with actual type `(a0, b2) -> b2'
    In the first argument of `fst', namely `snd'
    In the first argument of `(==)', namely `fst snd x'
    In the second argument of `(&&)', namely `fst snd x == b1'

main.hs:93:12:
    Couldn't match expected type `(a1, (Int, Int, Int) -> Int)'
                with actual type `(a2, b3) -> b3'
    In the first argument of `snd', namely `snd'
    In the expression: snd snd x
    In a stmt of a 'do' block:
      if fst x == a1 && fst snd x == b1 then snd snd x else 0

main.hs:97:10:
    Couldn't match expected type `(Int, b4)'
                with actual type `(Int, Int, Int)'
    In the first argument of `fst', namely `x'
    In the first argument of `(==)', namely `fst x'
    In the first argument of `(&&)', namely `fst x == a1'

main.hs:97:25:
    Couldn't match expected type `((Int, Int, Int) -> Int, b5)'
                with actual type `(a3, b6) -> b6'
    In the first argument of `fst', namely `snd'
    In the first argument of `(==)', namely `fst snd x'
    In the second argument of `(&&)', namely `fst snd x == b1'

main.hs:98:12:
    Couldn't match expected type `(a4, (Int, Int, Int) -> Int)'
                with actual type `(a5, b7) -> b7'
    In the first argument of `snd', namely `snd'
    In the expression: snd snd x
    In a stmt of a 'do' block:
      if fst x == a1 && fst snd x == b1 then
          snd snd x
      else
          firstlogic xs a1 b1

这段代码主要是使用生成的逻辑表作为函数来返回值 输入两个参数后的逻辑 代码:

comb0 :: [(Int, Int, Int)]
comb0 = do
   a <- [0,1,2]
   b <- [0,1,2]
   return (a, b, max a b)

firstlogic :: [(Int, Int, Int)] -> Int -> Int -> Int
firstlogic [] a1 b1 = 0
firstlogic [x] a1 b1 = do
  if fst x == a1 && fst snd x == b1
  then snd snd x
  else
    0
firstlogic (x:xs) a1 b1 = do
  if fst x == a1 && fst snd x == b1
  then snd snd x 
  else
    firstlogic xs a1 b1

main :: IO()
main = do 
  print firstlogic comb0 1 2

2 个答案:

答案 0 :(得分:4)

函数fst需要一对,而不是三元组。

fst :: (a, b) -> a

您需要一个不同的功能,fst3

fst3 :: (a, b, c) -> a

您可以编写此功能,也可以使用模式匹配/切换表达式。

fst snd x的外观也没有意义,因为fst只接受一个参数,而x仍然有(Int, Int, Int)类型。不确定你在这里想要完成什么。尝试使用模式匹配或切换表达式。

这并不是详尽无遗的。代码中还有其他问题。

答案 1 :(得分:2)

正如迪特里希已经指出你已经解决的主要问题,我认为我可以帮助你多一点。

这个将编译,我认为它做你想要的(虽然我不确定):

comb0 :: [(Int, Int, Int)]
comb0 = do
   a <- [0,1,2]
   b <- [0,1,2]
   return (a, b, max a b)

firstlogic :: [(Int, Int, Int)] -> Int -> Int -> Int
firstlogic [] _ _ = 0
firstlogic ((x1,x2,x3):xs) a1 b1
  | x1==a1 && x2==b1 = x3
  | otherwise = firstlogic xs a1 b1


main :: IO ()
main = 
  print $ firstlogic comb0 1 2

我不确定它是否符合您的要求的原因是我不明白您要做的事情 - 首先您将[0,1,2]的每个组合与其自身相结合,添加最大值这两部分然后您在firstlogic中搜索某个组合并再次返回max

如果我是对的,这个功能完全相同,但不需要comb0,也不需要搜索:

firstlogic :: Int -> Int -> Int
firstlogic = max

(好的:它不是检查两个输入是否在[0,1,2] - 但你可以轻松添加)