從這個 https://en.wikibooks.org/wiki/Haskell/Arrow_tutorial#Hangman:_Main_program哈斯克爾箭頭教程循環/狀態
如何在IO完成?特殊
main :: IO()
main = do
rng <- getStdGen
interact $ unlines -- Concatenate lines out output
. ("Welcome to Arrow Hangman":) -- Prepend a greeting to the output
. concat . map snd . takeWhile fst -- Take the [String]s as long as the first element of the tuples is True
. runCircuit (hangman rng) -- Process the input lazily
. ("":) -- Act as if the user pressed ENTER once at the start
. lines -- Split input into lines
交互似乎是(字符串 - >字符串) - > IO()。印象中它會打印每行讀取的函數。令我困惑的是,第一行是如何打印的。遊戲狀態在哪裏存儲?
runCircuit早些時候以其所有輸入已經生成的方式使用。我很困惑這個版本是如何逐行運行,但似乎沒有存儲任何狀態?
Circuit String (Bool, [String])
如何以逐行方式運行runCircuit :: Circuit a b -> [a] -> [b]
?似乎記得以前的結果在哪裏?
您的問題似乎與FRP和箭頭無關,無論您使用該示例的教程如何。請相應標記。 – leftaroundabout
狀態不一定要明確存儲在任何地方。它的作用與'State'完全一樣,它是一個函數 - >(a,s)'。 「國家」要簡單得多,所以不能認爲它應該更容易理解,但原則是完全一樣的。你鏈接的頁面給出了'runCircuit'死亡的簡單定義 - '\ cir - > snd。 mapAccumL uncircuit cir'。在這個定義中,它是'mapAccumL',它處理將每個先前狀態提供給後續定義。 'Circuit'本身就是一個有狀態計算的表示 - 所有的魔法都發生在'mapAccumL'裏面。 – user2407038