2016-08-15 24 views
1

我有這樣的全局狀態什麼是結合本地和全局狀態

type GlobalState a = State Int a 

而是一個功能需要自己的本地狀態,並獲得了GlobalState

type LocalState a = State [String] a 

但我的最佳方式現在不確定要合併它們。

現在我只需要添加本地狀態到全球

type GlobalState a = State (Int, [String]) a 

它工作正常,但我不認爲這是正確的,因爲我只需要在一個函數的局部狀態。 有沒有更好的方法?

回答

2

您可以使用兩個State s的monad stack

type LocalState a = [String] 
type GlobalState a = [String] 
newtype MyState a = StateT GlobalState (State LocalState) a 
+0

我可以用'GET'讀GlobalState但我怎麼能讀LocalState? – ais

+1

我在這裏創建了一個使用這種方法的示例:http://lpaste.net/177413 – ErikR

+0

@ais使用'lift'在內部monad中執行操作。 – arrowd