我正在尝试使用Haskell的Control.Monad.State
,尝试遍历一个字符串或整数的列表,对它们进行计数,并将字符串条目替换为整数0
。我已经成功地完成了计数部分,但在创建替换列表时失败了。下面是我的代码,它正确地将[3,6]
打印到屏幕上。我如何才能使它创建所需的列表[6,0,3,8,0,2,9,1,0]
?
module Main( main ) where
import Control.Monad.State
l = [
Right 6,
Left "AAA",
Right 3,
Right 8,
Left "CCC",
Right 2,
Right 9,
Right 1,
Left "D"]
scanList :: [ Either String Int ] -> State (Int,Int) [ Int ]
scanList [ ] = do
(ns,ni) <- get
return (ns:[ni])
scanList (x:xs) = do
(ns,ni) <- get
case x of
Left _ -> put (ns+1,ni)
Right _ -> put (ns,ni+1)
case x of
Left _ -> scanList xs -- [0] ++ scanList xs not working ...
Right i -> scanList xs -- [i] ++ scanList xs not working ...
startState = (0,0)
main = do
print $ evalState (scanList l) startState
字符串
1条答案
按热度按时间xyhw6mcr1#
[0] ++ scanList xs
不工作,因为scanList xs
不是列表,而是State (Int,Int) [Int]
。要解决这个问题,您需要使用fmap
/<$>
。您还需要更改基本情况,使状态值不成为返回值。
字符串
但是,为了进一步简化代码,最好使用
mapM
/traverse
和state
来删除递归和get
/put
语法的大部分样板。型