2014-11-15 89 views
-1

我有这样的代码,但这个错误被显示出来,我不知道为什么:我得到一个类型的错误,我不知道为什么

Couldn't match type 'Char' with '[Char]' 
    Expected type: [String] 
     Actual type: String 
    In the return type of a call of 'tarefa' 
    In the first argument of 'unlines', namely '(tarefa (lines inp))' 
    In the first argument of 'putStrLn', namely 
     '(unlines (tarefa (lines inp)))' 



main = do inp <- getContents; putStrLn (unlines (tarefa (lines inp))) 

tarefa :: [String]-> String 
tarefa tab = let board = getBoard tab 
       valid = valBoard board 
       ym = length board 
       xm = length (head board) 
       (r:p:s) = drop (length board) tab 
      in if valid/=0 then [show valid]     
          else if not (coordOK xm ym r) 
           then [show (ym +1)] 
           else if not (progOK p) 
             then [show (ym+2)] 
             else if s /= [] 
              then [show (ym+3)] 
              else ["OK"] 
+0

unlines有'[String] - > String'类型 – user1937198

回答

4

unlines预计字符串列表,它会加入'\n'字符。 tarefa的类型声明,但是,它说它返回一个单一的字符串。

看看你的实现,它看起来像tarefa是为了返回一个字符串列表;我想你只需要修改类型声明来反映这一点。

涉及字符串的类型错误可能会引起混淆,因为String[Char]的类型同义词,编译器在错误消息中指向哪个字符时不一致。

1

两个问题:

  1. 你觉得是这样的表达式的类型:

    [ “确定”]

(提示:你可以问ghci中)?

  1. 那么,为什么你声称你的函数返回String
相关问题