Haskell:我是在重新发明 List Monad 吗?

发布于 2024-12-20 07:21:57 字数 577 浏览 2 评论 0原文

在这里,完整的 Haskell 新手,我很抱歉......

我正在尝试从另一个序列和生成的最后一个值创建一个值序列(所以对我来说如何使用映射并不完全明显)。

在 clojure 中,我将使用循环构造,它基本上相当于递归函数。所以我想我可以用递归函数来解决这个问题

genSequence :: [a] -> [b] -> [a]
genSequence result [] = reverse result
genSequence a:as b:bs = genSequence ((computeNextA a b):a:as) bs

,我想这还不错(真正的函数当然更复杂......)但是我读了有关单子的内容(阅读 < a href="http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf" rel="nofollow">Philip Walder,然后是一些关于 monad 的内容clojure)并且情不自禁地觉得我应该在这里使用它们。不幸的是,到目前为止,我对单子的了解纯粹是理论上的,所以如果你能帮助我,我将非常感激。

Complete Haskell newbie here, my apologies....

I am trying to create a sequence of values out of another sequence and the last value generated (So it's not completely obvious to me how I would use map).

In clojure I would use a loop construct which is basically equivalent to a recursive function. So I thought I could use this problem with a recursive function along the lines of

genSequence :: [a] -> [b] -> [a]
genSequence result [] = reverse result
genSequence a:as b:bs = genSequence ((computeNextA a b):a:as) bs

and I guess this isn't so bad (the real function is of course more complicated ...) but I read about monads (read the excellent tutorial by Philip Walder, then some stuff on monads in clojure) and can't help the feeling that I should be using them here. So far my knowledge of monads is purely theoretical, unfortunately, so I would be very thankful if you could help me along.

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

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

发布评论

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

评论(1

囚你心 2024-12-27 07:21:57

不确定这是否有帮助,但类似的东西(假设 computeNextA+

genSequence [4] [60,70,80,90]
--[4,64,134,214,304]

相当于

scanl (+) 4 [60,70,80,90]
--[4,64,134,214,304]

Not sure if this helps, but something like (assuming that computeNextA is +)

genSequence [4] [60,70,80,90]
--[4,64,134,214,304]

is equivalent to

scanl (+) 4 [60,70,80,90]
--[4,64,134,214,304]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文