我仍然是哈斯克尔的新手,我认为我现在已经过了头。我的代码如下所示。复杂状态单体结构
data World = World {
intStack :: [Int],
boolStack :: [Bool]
} deriving Show
instance IntStack World where
getIntStack = intStack
putIntStack ints (World _ bools) = World ints bools
instance BoolStack World where
getBoolStack = boolStack
putBoolStack bools (World ints _) = World ints bools
class IntStack a where
getIntStack :: a -> [Int]
putIntStack :: [Int] -> a -> a
class BoolStack a where
getBoolStack :: a -> [Bool]
putBoolStack :: [Bool] -> a -> a
(<=>) :: (IntStack c, BoolStack c) => c -> c
(<=>) w = putIntStack xs . putBoolStack ((x == x'):bs) $ w
where (x:x':xs) = getIntStack w
bs = getBoolStack w
(<+>) :: (IntStack c) => c -> c
(<+>) w = putIntStack ((x+x'):xs) w
where (x:x':xs) = getIntStack w
我的焦点(目前忽略的功能的错误情况)是能够链接在一起的功能,如(< =>)和(< +>)假设底层数据类型实现了功能的必需的接口。
我觉得我可以打扫一下了很多有状态单子,但我不知道如何构建它允许更改实现IntStack,BoolStack等任何数据类型..
我知道这是一个非常模糊的描述,但我觉得我上面的代码可能是完全错误的方法。
感谢您的任何反馈!
耶,我也(重新)发明了镜头!我现在正在从hackage中拉出软件包。 +1 – pat
非常感谢,绝对让我感到有用的是这些。 –