- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过构建自己的示例来理解 Haskell 中 State Monad 的一些基础知识。
考虑一个简单的例子,我想计算一个整数数组中偶数的个数。当然这可以使用纯函数很容易地完成,但我想尝试循环状态 monad 路线,我们保留一个计数器,该计数器为每个已检查的元素不断递增。
这是迄今为止我设法提出的部分(但显然是错误的)尝试。
import Control.Monad.State
f' :: [Int] -> State Int [Int]
f' [] = state (\s -> ([],s))
f' (x:xs) = if x `mod` 2 == 0 then state (\s -> ((x:xs), s+1)) -- how can I fix this line?
else f' xs
这段代码可以编译,但显然没有给出正确答案。那么我该如何修复此代码,以执行类似于以下 Python 代码的操作
counter = 0 # incremented when we encounter an even integer.
for elt in integer_list:
if elt % 2 == 0 :
counter = counter + 1
最佳答案
另一个答案是从头开始构建一个实现。我认为也值得看到对现有代码进行最小的更改以使其变得合理。我们甚至会保留您现有的类型——尽管另一个答案建议对其进行更改,但我认为这是可以接受的(如果不是很好的话)。
在我看来,真正的问题在于您只在 if
的一个分支中进行了递归。我们真正想要的是递归当前元素是否为偶数。所以:
f' (x:xs) = do
if x `mod` 2 == 0 then state (\s -> ((), s+1)) -- increase the count by one
else state (\s -> ((), s )) -- don't increase the count by one
rest <- f' xs -- compute the answer for the rest of the list
return (x:rest) -- reconstruct the answer for the whole list
我们可以在 ghci 中检查它是否做了正确的事情:
> runState (f' [1..5]) 0
([1,2,3,4,5],2)
这只是让您的实现想法发挥作用所能做出的最小改变。
从那里,我会建议一些重构。首先,您对 state
的普遍使用是一种代码味道。我会用这种方式写出各种用途:
f' [] = return []
f' (x:xs) = do
if x `mod` 2 == 0 then modify (+1) else return ()
rest <- f' xs
return (x:rest)
从这里开始,我将在条件语句中使用 even
函数,并注意到 when
函数实现了“执行某些操作或 return ()
”操作。所以:
f' [] = return []
f' (x:xs) = do
when (even x) (modify (+1))
rest <- f' xs
return (x:rest)
此外,我们实际上有一个组合器,用于对列表的每个元素运行单子(monad)操作;即 mapM
。所以我们可以这样把上面的显式递归变成隐式递归:
f' xs = mapM (\x -> when (even x) (modify (+1)) >> return x) xs
最后,我认为函数返回它使用的列表有点奇怪。 unidiomatic,就其本身而言,正如之前的反对意见一样,但也许不是您想要的。如果事实证明您不打算在任何后续计算中使用结果列表,那么随手丢弃它会更有效;而 mapM_
组合器就是这样做的。所以:
f' :: [Int] -> State Int ()
f' xs = mapM_ (\x -> when (even x) (modify (+1))) xs
在这一点上,我认为 f'
是您提出的想法的一个很好的实现。
关于haskell - 如何使用 State monad 计算 Haskell 列表中偶数的个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39990592/
我有一个包含多个数字的数组: int[] tab = {1,2,3,4}; 我必须创建两个方法,第一个是 sum() 方法,第二个是 numberOdd()。 这一步就OK了! int length
我一直在研究这个简单的待办事项列表,现在正处于调试阶段。当我实现不同的更改时,我观察到以下情况 保留列表中的所有项目:我添加 1 项,没问题。我添加了 2 个项目,但没有任何效果 - 无法标记完整/不
我想仅在未引用的术语中将 | 替换为 OR,例如: "this | that" | "the | other" -> "this | that" OR "the | other" 是的,我可以分割空格
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我在一些练习中得到了这个示例代码,它展示了如何查找整数是奇数还是偶数。 int x = 4; if ( (x & 1) == 0 ) { System.out.prin
我无法制作简单的 JavaScript 来获取文本框中的数字,并在单击按钮时返回结果。 function check() { var v=document.getElem
我是一名初学者,我正在尝试创建一个程序,该程序将生成仅由偶数组成的数字,并四舍五入到最接近的偶数。 例子: 135 --> 200 2700 --> 2800 我有一个代码,但每当我尝试打印它时,数字
我正在尝试编写一个程序,它将接受两个数字并根据它们的值返回两个都是奇数,两个都是偶数,或者 A 是奇数而 B 是偶数,等等。 我设法让程序检查一个变量,但如果我添加第二个变量,我会得到一个我不期望的输
这是一个带有数据和下一个属性的标准链表。 这就是我正在尝试的: class Node { constructor(data, next) { this.data = data;
我有一个正则表达式问题,我不知道该怎么做。它必须匹配开头包含任意数量的 a 的所有字符串,然后如果 a 的数量是偶数则匹配单个 0,如果 a 的数量是奇数则匹配单个 1。 如何跟踪偶数/奇数? 示例
我试图在数组中执行一个循环,对内容进行排序并创建每个具有 2 个值的 div。 尝试了很多东西,但我不知道我需要做什么。 这就是我需要做的:在数组中循环并创建 div。每个 div 应该有 2 个数组
您好,想知道是否有更简单的方法来显示奇数/偶数。我知道我可以做一个 for 循环并加载一个列表。然后我可以写另一个for循环遍历列表并检查值是否为奇数/偶数: for(i=0; i i % 2 ==
我正在尝试改变边距以使图像上下呈之字形。我发现this article很接近,但将一项更改应用于所有 .brochureImg 类。我做错了什么? HTML GP &
我编写这个方法来重新排列两个堆栈的元素,使堆栈 s1 只包含偶数整数,堆栈 s2 只包含奇数整数。并且 s1 或 s2 中不应存储任何零。 public static void rerange(sta
我需要帮助才能将未知整数分成给定数量的偶数部分——或者至少尽可能地均匀。各部分之和应为原值,但各部分应为整数,且应尽可能接近。 参数 num: Integer - 应该被分成相等部分的数字 parts
我在这里想做的是由偶数消费者打印偶数,由奇数消费者打印奇数。 有一个 evenodd 方法,它基本上消耗任何数字并打印(无论是偶数还是奇数)。我有 2 个偶数消费者线程、2 个奇数消费者线程、2 个偶
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
谁能帮忙。我必须编写一个程序,让用户输入 10 个数字。并将这些数字按奇数(升序)和偶数(降序)排序。 例如:输入:2、8、7、13、23、9、34、11、1、16输出:1、7、9、11、13、23、
给定两个列表,我想合并它们,以便第一个列表中的所有元素都是偶数索引(保留它们的顺序),第二个列表中的所有元素都是奇数索引(也保留它们的顺序)。示例如下: x = [0,1,2] y = [3,4] r
我有一个表,对列和行进行求和,并显示求和的结果。我必须改变每个总数的颜色。如果是偶数,则将其设置为“绿色”。如果是奇数,则将其设置为“红色” 这是我的 table :
我是一名优秀的程序员,十分优秀!