English 中文(简体)
4. 在Haskell州提起的诉讼案件
原标题:Functor / Applicative instances for State in Haskell

在阅读瓦德尔关于僧mon的论文(并 sections一些部分)之后,我决定通过该文件更加密切地工作,为他所描述的每个僧.确定ctor的遗嘱和参考案例。 使用同义词

type M a = State -> (a, State)
type State = Int

瓦勒用于界定国家僧mon,我有以下(使用相关名字,以便我稍后能够用新的类型声明来界定这些名字)。

fmap  :: (a -> b) -> M a -> M b
fmap  f m = st -> let (a, s) = m st in (f a, s)

pure  :: a -> M a
pure  a = st -> (a, st)

(<@>) :: M (a -> b) -> M a -> M b
sf <@> sv = st -> let (f, st1) = sf st
                       (a, st2) = sv st1
                    in (f a, st2)

return  :: a -> M a
return  a = pure  a

bind :: M a -> (a -> M b) -> M b
m `bind` f = st -> let (a, st1) = m st
                        (b, st2) = f a st1
                     in (b, st2)

当我转向在新的类型声明中使用一种类型构造,例如

newtype S a = S (State -> (a, State))

一切都相距甚远。 一切只是略有改动,例如,

instance Functor S where
 fmap f (S m) = S (st -> let (a, s) = m st in (f a, s)) 

instance Applicative S where
 pure a = S (st -> (a, st))

但是,由于lam布达的表述是隐藏在这种建筑内,因此GHC没有发生任何事情。 现在,我看到的唯一解决办法是确定一项职能:

isntThisAnnoying s (S m) = m s

a. 保证金的固定价值和实际收益,例如:

fmap f m = S (st -> let (a, s) = isntThisAnnoying st m in (f a, s))

是否有另一种方式不使用这些辅助职能?

最佳回答

如果你看here,你将看到它们这样界定:

newtype State s a = State { runState :: (s -> (a,s)) }

为了给内人一个名字。

问题回答

The usual way is to define newtype newtype S a = S {runState : State -> (a, State)}. Then instead of your isntThisAnnoying s (S m) you can write runState t s where t is the same as S m.
You have to use a newtype because type synonyms cannot be typeclass instances.





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签