标签: state-monad

在Haskell中没有更多状态monad版本的哈希映射/集合?

散列集和映射的monadic接口是否在Haskell中消失了?在使用现代版本时,我应该考虑什么样的性能模型?(Data.Map,Data.HashMap,Data.HashSet).它们在我的版本中似乎没有任何IO代码(ghc 7.0.2),

> :browse Data.HashSet
type HashSet a = Set a
newtype Set a
  = Data.HashSet.Set (Data.IntMap.IntMap (Data.HashSet.Some a))
(\\) :: Ord a => Set a -> Set a -> Set a
delete :: (Data.Hashable.Hashable a, Ord a) => a -> Set a -> Set a
difference :: Ord a => Set a -> Set a -> Set a
elems :: Set a -> [a]
empty :: Set a
Data.HashSet.filter :: Ord a => (a -> Bool) -> Set a …
Run Code Online (Sandbox Code Playgroud)

hash haskell state-monad

1
推荐指数
2
解决办法
877
查看次数

如何在Haskell中无限期地在同一个State monad中连接一个状态?

从Haskell开始,一直停留在State Monad ......

所以我试图在Haskell中掌握State Monad,并理解它,我正在编写一个代码来生成PRBS序列.对于感兴趣的人,它在论文'Pseudo Random Sequences and Arrays'中描述,其免费副本可以通过doi获得:10.1109/PROC.1976.10411.

必要的代表团很简单.你有一个移位寄存器和一个生成多项式.生成多项式告诉您移位寄存器的哪些位采用并求和(模2)以获得移位寄存器的MSB.在下一次迭代中,您采用此计算的MSB并执行右移操作后将其粘贴到移位寄存器的MSB .

没有monad这样做的代码非常简单:

import Data.List

m = (4::Int)              -- This is the degree of the polynomial
p = tail $ [4, 1, 0]      -- This is a actual polynomial
s = 1 : replicate (m-1) 0 -- This is the initial state. Note that the head is the MSB

chSt poly s = msb poly s : init s -- changes the shift …
Run Code Online (Sandbox Code Playgroud)

haskell state-monad

1
推荐指数
1
解决办法
142
查看次数

1
推荐指数
1
解决办法
133
查看次数

Monad的最佳实践

我想知道什么可以被视为关于Statemonad 的最佳实践.我也对任何其他建议持开放态度.

我有一个要解析的二进制文件.它包含需要解析的不同标头,以便能够读取整个文件.

因此,只能使用解析中的State来解析头文件.

data ParseState = ParseState {
   offset :: Int64
   buffer :: B.ByteString
   endianness :: Endianness
   pointerSize :: MachineWord
   positionStack :: [Int64]
}
Run Code Online (Sandbox Code Playgroud)

然后将该数据用于Statemonad中

type Parser a = State ParseState a
Run Code Online (Sandbox Code Playgroud)

这可以完美地解决标头的解析.但是,只要我想解析整个文件,我需要标题中的信息才能正确读取文件.

data Header = Header {
    txtOffset :: Int64,
    stringOffset :: Int64
}
Run Code Online (Sandbox Code Playgroud)

我需要标头信息来继续解析文件.

我的想法是使用一个位于前一个状态之上的新状态monad.所以我有一个新的StateT monad:

type ParserFullState a = StateT Header (State ParserState) a
Run Code Online (Sandbox Code Playgroud)

因此,我可以继续使用新的状态转换器构建一整套解析器函数.我也可以采用不同的方式将标题添加到原始ParseState数据中.

我可以看到将标题添加回ParserState以下内容的优点如下:

  1. 解析器函数的返回类型是统一的
  2. 无需调用lift来访问解析器原语.

我能看到的缺点是:

  1. 高级解析器和低级原语之间没有区别.
  2. 我们无法清楚地说明标头何时完全解析或何时不完整.从而使解析器修改更加脆弱.

你的建议是什么?我应该使用状态转换器吗?我应该将标头添加到原始状态还是其他任何情况?

谢谢.

monads haskell state-monad

1
推荐指数
1
解决办法
243
查看次数

F# 中的随机/状态工作流

我正在尝试围绕 F# 中的 mon-、err、工作流进行思考,虽然我认为我对基本的“可能”工作流有相当深入的了解,但尝试实现状态工作流来生成随机数确实让我受益匪浅难倒。

我未完成的尝试可以在这里看到:

let randomInt state =
    let random = System.Random(state)
    // Generate random number and a new state as well
    random.Next(0,1000), random.Next() 

type RandomWF (initState) =
    member this.Bind(rnd,rest) =
        let value, newState = rnd initState
        // How to feed "newState" into "rest"??
        value |> rest
    member this.Return a = a // Should I maybe feed "initState" into the computation here?

RandomWF(0) {
    let! a = randomInt
    let! b = randomInt
    let! c = randomInt
    return [a; b; …
Run Code Online (Sandbox Code Playgroud)

monads f# state state-monad

1
推荐指数
1
解决办法
434
查看次数

链接状态单子

我有一个功能

step :: Int -> State Int Int
step n = get >>= \x ->  put  (x `div` n) >> return (x `mod` n)

?> runState (step 25) 41
(16,1)
Run Code Online (Sandbox Code Playgroud)

如何运行step具有不同值的 s序列n使用上一步的状态和每个步骤?

所以例如步骤如下

第一步产生(16,1),然后我想将其用作下一步n = 10应该产生的输入(6, 2)。将第一步中的 1 添加到其中,并将第一步中的 16 与新的 n 相加。

n = 25 gives (16,1) then
n = 10 gives (6,2)  then
n = 5  gives (1,3) then
n = 1 gives (0,4)
Run Code Online (Sandbox Code Playgroud)

我知道使用 State在这里可能不正确;但我试图用它作为一种学习方式。

可能的目的是用状态 …

monads haskell state-monad

1
推荐指数
1
解决办法
336
查看次数

Haskell MTL:如何退出monad并获取其中的值?

我知道如何使用do块内每个monad的函数.但是一旦我完成了如何运行计算并获得结果?

run :: (MonadError Error m, MonadState State m) => m Int -> Int
run = ???
Run Code Online (Sandbox Code Playgroud)

monads haskell typeclass state-monad monad-transformers

1
推荐指数
1
解决办法
174
查看次数

monad变换器堆栈中的evalState

给定mtlmonad堆栈,例如ExceptT String (WriterT String (State s a)),如何评估内部状态monad而无需打开外部monad?

have :: ExceptT String (WriterT String (State s)) a
f    :: State s a -> a

want :: ExceptT String (WriterT String Identity) a
Run Code Online (Sandbox Code Playgroud)

我可以通过调用runExceptT后跟runWriterT并重新打包结果来实现这一点,但这似乎是完成此任务的错误方法.


就我所尝试而言,类似fmap或类似的东西将无法工作,因为monad变换器堆栈被视为一个完整的monad.我需要的是一个"拆分"monad变换器堆栈的功能,如下所示:

split :: (MonadTrans s, Monad t) => (s t) a -> s (t a)
Run Code Online (Sandbox Code Playgroud)

要么我没有找到这个功能,要么解决方案完全不同.

haskell state-monad monad-transformers

1
推荐指数
1
解决办法
135
查看次数

Getting Gamestate value in State Monad

I'm learning about state monads and i'm a little confused.

I have a data type

data GameState = GameState (Map String Double) Double Bool
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)

The second argument Double is a direction

and of course the state monad definition

newtype State s a = StateOf (s -> (s, a))
deState (StateOf stf) = stf
get = StateOf (\s0 -> (s0, s0))
put s = StateOf (\s0 -> (s , ()))
modify f = get >>= \s -> put …
Run Code Online (Sandbox Code Playgroud)

haskell state-monad

1
推荐指数
1
解决办法
41
查看次数

Make Haskell Imperative

Note: this is an exercise, and I'm trying to understand how things work.

I am trying to make it possible to do something like this in Haskell:

f :: Integer -> Integer
f n = def $ do
  i      <- var n
  while i (>0) $ do
    i      -= lit 1
  return i
-- for now, my program returns n 
Run Code Online (Sandbox Code Playgroud)

Below is my program. At this point, I cannot understand why it doesn't work, but for some reason, the variables …

haskell imperative state-monad

1
推荐指数
1
解决办法
71
查看次数