我有一個字符串列表,並試圖這樣:Haskell:不能使用「map putStrLn」嗎?
ls = [ "banana", "mango", "orange" ]
main = do
map PutStrLn list_of_strings
這並沒有工作,我不明白爲什麼。
ghc print-list.hs
print-list.hs:3:0:
Couldn't match expected type `IO t' against inferred type `[IO()]'
In the expression: main
When checking the type of the function `main'
任何提示?我想它與地圖返回一個列表而不是一個值,但我沒有找到一個簡單的方法來解決這個問題。
現在我知道打印字符串列表的唯一方法是編寫一個迭代列表的函數,打印每個元素(如果列表是[a]則打印,但如果列表是[a]則打印並遞歸) b))。但它會多更簡單的只是使用地圖...
謝謝!
我希望自己早點學到的東西是在Data.Traversable中定義了另一個mapM。這個mapM除了列表之外還有更多的數據結構,比如地圖和數組。 – 2009-05-31 20:56:19