函式定義(>>)如下:
(>>) :: Monad m => m a -> m b -> m b
但我想實作這個功能翻轉如下:
我有一個函式,它將串列列印為具有給定列數的表,并回傳monadtabulate :: Int -> [Int] -> IO Int中所有串列項的總和。IO之后我想要一個明確的putStr "\n".
如果我會使用以下內容:
tabulate >> (putStr "\n")
它會丟棄制表的結果,反之則不會在表格后列印換行符。如果這樣做do:
smth = do
let a = tabulate
putStr "\n"
a
這樣做會在表格之前再次列印換行符,因為a在putStr.
在制表功能之后如何列印換行符?
uj5u.com熱心網友回復:
你可以在(<*) :: Applicative f => f a -> f b -> f a這里作業:
smth :: IO Int
smth = tabulate 14 [2, 5] <* putStr "\n"
這相當于:
smth = do
a <- tabulate 14 [2, 5]
putStr "\n"
return a
因此,它首先評估 的IO Int,tabulate 14 [2, 5]然后作為操作列印"\n",但它“回傳”呼叫的值tabulate,而不是putStr呼叫的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/417807.html
標籤:
