[問題] Lazy Evaluation?
剛剛在練習寫 Haskell 時,遇到個小問題,一時不解上來發問。
我想寫一個 function 用來讀 stdin 直到讀到特定的字元
成功版:
getCharUntil :: Char -> IO [Char]
getCharUntil c = do x <- getChar
if x == c then return []
else do xs <- getCharUntil c
return $ x : xs
失敗版:
sequenceWhile :: Monad m => (a -> Bool) -> [m a] -> m [a]
sequenceWhile f ms = sequence ms >>= return . takeWhile f
getCharUntil' c = sequenceWhile (/=c) $ repeat getChar
失敗版會一直讀不停
Lazy evaluation 很耐死的,一定是有什麼誤會…
感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 71.202.208.114
推
11/13 05:53, , 1F
11/13 05:53, 1F
推
11/13 14:10, , 2F
11/13 14:10, 2F
→
11/13 14:34, , 3F
11/13 14:34, 3F
→
11/13 14:36, , 4F
11/13 14:36, 4F
推
11/13 20:29, , 5F
11/13 20:29, 5F
→
11/14 06:13, , 6F
11/14 06:13, 6F
推
11/14 06:19, , 7F
11/14 06:19, 7F
→
11/14 15:10, , 8F
11/14 15:10, 8F
推
11/14 21:13, , 9F
11/14 21:13, 9F
→
11/14 21:15, , 10F
11/14 21:15, 10F
→
11/14 21:16, , 11F
11/14 21:16, 11F
→
11/14 21:17, , 12F
11/14 21:17, 12F
→
11/14 21:17, , 13F
11/14 21:17, 13F
→
11/14 21:19, , 14F
11/14 21:19, 14F
→
11/14 21:19, , 15F
11/14 21:19, 15F
→
11/14 21:19, , 16F
11/14 21:19, 16F
→
11/14 23:24, , 17F
11/14 23:24, 17F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
PLT 近期熱門文章
PTT數位生活區 即時熱門文章