我寫了個小程序在Haskell使用狀態單子帶矢量計算int類型的所有ocurences在樹: import Data.Vector
import Control.Monad.State
import Control.Monad.Identity
data Tree a = Null | Node (Tree a) a (Tree a) deriving Show
main :: IO()
如何可以寫出如下功能tt,其目前已鍵入錯誤: t :: Int
t = runST $ do
ref <- newSTRef 10
readSTRef ref
tt :: (STRef s a -> ST s a) -> Int
tt f = runST $ do
ref <- newSTRef 10
f ref
ttTest = tt read
我最近開始在Hackage上查看核心庫,並且有一個我不明白的反覆出現的習語。下面是來自ST module一個例子: instance Monad (ST s) where
{-# INLINE (>>=) #-}
(>>) = (*>)
(ST m) >>= k
= ST (\ s ->
case (m s) of { (# new_s, r