2014-12-25 40 views
2

類型:含義在lambda表達式分號

data Command a = Command String (a -> IO a) 

功能:

iofunc_ :: String -> (a -> IO()) -> Command a 
iofunc_ s f = Command s (\x -> do f x ; return x) 

什麼是分號lambda表達式(\x -> do f x ; return x)怎麼辦?

+3

它是「do」符號的一部分,而不是lambda。 – augustss

回答

7

他們只是將二者分開表達f xreturn x中做記號。其實這些都是你的情況相當於:

iofunc_ s f = Command s (\x -> do f x ; return x) 

iofunc_ s f = Command s (\x -> do {f x ; return x}) 

iofunc_ s f = Command s (\x -> do f x 
            return x) 

iofunc_ s f = Command s (\x -> f x >> return x) 
+1

什麼時候可以省略大括號?我一直認爲它們是強制性的,如果你明確地使用分號。 – kqr

+0

@kqr沒看過[Haskell的報告(https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-210002.7)完全,但你可以看到這是他們在與例子已經證明'let'。 – Sibi

+0

@Sibi謝謝。 – Stanko