2014-11-01 51 views
1

我貫通進入「學習你一些哈斯克爾」,我已經寫了以下應用:

import System.IO 

main = do 
    filename <- getLine 
    handle <- openFile filename ReadMode 
    content <- hGetContents handle 
    putStr . unlines . (map isLong) . lines $ content 
    hClose handle 

isLong :: String -> String 
isLong x = if length x > 10 then x ++ " long enough" else x ++ " could be better!" 

和它的作品,但是當我刪除行和內容之間的「$」彙編失敗。

你能幫我理解爲什麼這是錯的嗎?我正在考慮用點組成語句,我得到一個函數(String - > IO()),並將其應用於「內容」,但爲什麼在這裏需要「$」?

謝謝!

回答

8

運算符(。)的類型爲(b -> c) -> (a -> b) -> a -> c ....其前兩個輸入必須是函數。

lines content,但是,類型[String],不是一個函數,因此f . lines content將失敗。編譯器將其視爲

f . (lines content) 

通過添加($),您可以更改優先級,併成爲

f . lines $ content = (f . lines) $ content 

它的工作原理,因爲flines是兩種功能。

0

haskell中的美元符號用於在值上應用函數。

它的背景是,你不需要複雜的括號。