2016-06-13 52 views
0

我是Haskell的新手,尝试实现here中的代码以使用地图替换字符串。我汇编,说使用地图编译错误替换字符串

* Expecting one more argument to `StringMap' 
    Expected a type, but `StringMap' has kind `* -> *' 
* In the type signature: 
    stringMapReplace :: (Show stringMap) => StringMap -> String -> String 

我试图寻找过程中收到错误信息,但我可以找到错误的唯一的答案是,我不是澄清StringMap是什么类型。不过,我认为这是Show stringMap正在做的。

import Data.Map 
import Data.Strings 

type StringMap stringMap = [(String, String)] 
myStringMap = 
    [ 
     ("org1", "rep1"), 
     ("org2", "rep2") 
    ] 

stringMapReplace :: (Show stringMap) => StringMap -> String -> String 
stringMapReplace [] s = s 
stringMapReplace (m:ms) s = strReplace ms (replace (fst m) (snd m) s) 

main :: IO() 
main = do 
    putStrLn "Enter some text:" 
    putStrLn =<< stringMapReplace myStringMap <$> toUpper getLine 

注:strReplace来自Data.Strings

我不知道是否有别的错误的代码,编译器只给上面,现在的错误。如果您注意到其他任何事情,请随时提及(或稍后作为练习留待我调试)。

回答

4

您将类型同义词StringMap定义为采用(未使用的)类型参数stringMap。类型的同义词,而不是newtype,data和GADT声明,必须始终为完全应用。因此,每次出现StringMap必须提供一个参数,如forall a . StringMap a,StringMap Int等。在stringMapReplace的签名中,您不给StringMap一个参数,因此是错误。

两个选项:

  1. 变化StringMaptype StringMap = [(String, String)],因为它并不需要的参数。
  2. 给出StringMap参数stringMapReplace的签名。你问什么参数?任何一个,因为它被忽略。例如,下面应该工作:

    stringMapReplace :: StringMap String -> String -> String 
    
+0

@chi感谢。现在修复。 – crockeea