lambda表达式中分号的含义

时间:2014-12-25 11:54:28

标签: haskell lambda syntax

输入:

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)中做了什么?

2 个答案:

答案 0 :(得分:8)

他们只是将两个表达式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 :(得分:1)

Semicolon anywhere is equivalent to a change of line indented to the same level as the previous valid expression.

I saw it by going through how indentation works (https://en.wikibooks.org/wiki/Haskell/Indentation).