gpt4 book ai didi

haskell - 提供 monad 堆栈的测试实现

转载 作者:行者123 更新时间:2023-12-02 14:22:26 26 4
gpt4 key购买 nike

这是我的程序中构建的 monad 堆栈:

type Px a = ReaderT PConf (State PState) a

其中 PConf 和 PState 是保存应用程序的配置和状态的任意数据类型。

我需要提供一个默认实现,以便我可以在给定“PConf”和“PState”的情况下测试我的函数:

p4 :: Px ()
p4 = ???
where
conf = PConf 4 10 10 [0, 1]
state = PState 0 0 $ M.fromList [((x, y), Nothing) | x <- [0..9], y <- [0..9]]

我不知道在“p4 =”中写什么,以便实现使用“conf”和“state”。谢谢。

最佳答案

如果 p4Px () 操作,那么这不是您定义 conf 的点传入,如果 state 应该是初始状态,则也不是那个状态。相反,您应该将它们放在测试 p4 的函数中:

p4 :: Px ()
p4 = ...

test = runState (runReaderT p4 conf) state
where
conf = PConf 4 10 10 [0, 1]
state = PState 0 0 $ M.fromList [((x, y), Nothing) | x <- [0..9], y <- [0..9]]

编辑:如果您的问题仍然是编写 p4 本身,这里有一个小模型 p4 ,它更改了一些状态以包含一些原始的 配置文件:

p4 = do
PConf x y z _ <- ask
PState m n mp <- get
put $ PState m n (M.insert (x, y) (Just z) mp)

关于haskell - 提供 monad 堆栈的测试实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com