有没有办法对monad中存储的值执行一个案例,而不必将名称绑定到它?
即而不是这样做:
c <- getChar
case c of
...
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点:
mcase getChar of
...
Run Code Online (Sandbox Code Playgroud)
或者,如果case语句可以部分应用,那将是很好的:
case of
...
Run Code Online (Sandbox Code Playgroud)
会不情愿地:
\a -> case a of
...
Run Code Online (Sandbox Code Playgroud)
所以你可以这样做:
getChar >>= case of
...
Run Code Online (Sandbox Code Playgroud) 我有一个yaml文件:
base123:
key1: "key1"
key2: "key2"
key3: "key3"
Run Code Online (Sandbox Code Playgroud)
和代码,它被允许从中读取所有3个值:
read123 :: IO (String, String, String)
read123 = do
rawConfig <- Y.decodeFile "config.yml" :: IO (Maybe Y.Value)
case rawConfig of
Just res1 ->
case res1 of
Object res2 ->
case (LHashMap.lookup "base123" res2) of
Just (Object res3) ->
case (LHashMap.lookup "key1" res3) of
Just (String key1) ->
case (LHashMap.lookup "key2" res3) of
Just (String key2) ->
case (LHashMap.lookup "key3" res3) of
Just (String key3) -> return (key1, key2, key3)
_ -> …
Run Code Online (Sandbox Code Playgroud) haskell ×2