了解 Haskell 访问器函数

发布于 2024-09-09 04:03:57 字数 530 浏览 4 评论 0原文

我正在阅读 Monad 教程,我现在正在研究的教程是 http:// www.muitovar.com/monad/moncow.xhtml ,但是我遇到了状态 Monad 的问题,或者更准确地说是 runState 访问器函数的问题。

类型被定义为

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

并且被称为例如

runState (chncasewst3 'e' 'd' 'f') False

我不知道如何阅读定义以到达第二行,特别是因为“State s a”部分。如果它是“State a s”,我可以推断出访问器已经被柯里化到“s”。

所以问题是;如何读取类型定义,以便我可以了解如何在这种情况下调用访问器函数,以及如果可能的话如何读取访问器函数本身。

I'm reading up on Monad tutorials, and the one I'm working on now is http://www.muitovar.com/monad/moncow.xhtml , but I ran on a problem with the state Monad, or to be more precise the runState accessor function.

The type is defined as

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

and it's called e.g.

runState (chncasewst3 'e' 'd' 'f') False

I don't know how to read the definition for getting to the second line, especially because of the "State s a" part. If it where "State a s", I could deduce that the accessor has been curried 'as far' as the 's'.

So the question is; how to read the type definition so that I could see how to call the accessor function in this situation, and if possible how to read accessor functions per se.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2024-09-16 04:03:57

当您将数据类型定义为像

data T a b = MkT { getA :: a, getB :: b }

一样读取它时

data T a b = MkT a b

自动定义两个辅助函数

getA :: (T a b) -> a
getA (MkT x _) = x

getB :: (T a b) -> b
getB (MkT _ y) = y

:当您将 getA 应用于 T 的值时,结果的类型为 一个。

现在,您的 State 类型仅包含一个元素,该类型是一个函数 (:: s -> (a, s))。 runStateState s a 类型的值转换为该类型的函数。

ghci> :t runState
runState :: State s a -> s -> (a, s)

每次将 runState 应用于 State s a 类型的值时,结果都是一个 s ->; 类型的函数。 (a,s)。该函数的第一个参数是状态变量(s 类型)的初始值。

在教程示例中,

  • chncasewst3 'e' 'd' 'f' 的类型为 State Bool String
  • 因此,runState (chncasewst3 'e' 'd' 'f') 的类型为 Bool ->; (字符串,布尔值)
  • 因此,runState (chncasewst3 'e' 'd' 'f') False 的类型为 (String, Bool)

进一步阅读:

When you have a data type defined as

data T a b = MkT { getA :: a, getB :: b }

read it like

data T a b = MkT a b

with two helper functions defined automatically:

getA :: (T a b) -> a
getA (MkT x _) = x

getB :: (T a b) -> b
getB (MkT _ y) = y

When you apply getA to the value of T, the result is of type a.

Now your State type consists of only one element, which type is a function (:: s -> (a, s)). runState converts a value of type State s a to a function of this type.

ghci> :t runState
runState :: State s a -> s -> (a, s)

Every time you apply runState to the value of type State s a, the result is a function of type s -> (a,s). And the first argument of this function is an initial value of the state variable (of type s).

In the tutorial example,

  • chncasewst3 'e' 'd' 'f' has type State Bool String.
  • So, runState (chncasewst3 'e' 'd' 'f') has type Bool -> (String, Bool).
  • So, runState (chncasewst3 'e' 'd' 'f') False has type (String, Bool).

Further reading:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文