- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码:
divisibleBy :: Int -> Int -> Bool
divisibleBy x y
| mod x y == 0 = True
| otherwise = False
isEven :: Int -> Bool
isEven x
| (divisibleBy x 2) == 0 = True
| otherwise = False
错误:
practical1.hs:30:28: error:
• No instance for (Num Bool) arising from the literal ‘0’
• In the second argument of ‘(==)’, namely ‘0’
In the expression: (divisibleBy x 2) == 0
In a stmt of a pattern guard for
an equation for ‘isEven’:
(divisibleBy x 2) == 0
|
30 | | (divisibleBy x 2) == 0 = True |
divisibleBy
函数有效,但 isEven
函数无效。我做错了什么?
最佳答案
错误消息已经说明了这一点。你写:
isEven :: Int -> Bool
isEven x
<b>| (divisibleBy x 2) == 0 = True</b>
| otherwise = False
现在,如果我们对此进行类型检查,我们会看到 (divisibleBy x 2)
将返回 Bool
,并且您无法执行 (==)
code> 带有一个 Bool
和一个数字(Bool
在 Haskell 中不是数字)。
为什么用 == 0
写这个对我来说并不是很清楚,我们可以将其写为:
isEven :: Int -> Bool
isEven x
<b>| (divisibleBy x 2) = True</b>
| otherwise = False
但现在它仍然不优雅:我们不必检查条件是否成立返回 True
否则 False
,我们可以简单地返回定义,所以:
isEven :: Int -> Bool
isEven x = divisibleBy x 2
或者我们可以通过使用 flip::(a -> b -> c) -> b -> a -> c
来省略 x
参数:
isEven :: Int -> Bool
isEven = flip divisibleBy 2
同样适用于divisibleBy
函数,我们可以将其重写为:
divisibleBy :: Int -> Int -> Bool
divisibleBy x y = mod x y == 0
或者不带参数:
divisibleBy :: Int -> Int -> Bool
divisibleBy = ((0 ==) .) . mod
divisableBy
的参数它看起来更Haskell来交换函数的参数,所以我们可以将其写为:
divisibleBy :: Int -> Int -> Bool
divisibleBy x y = mod y x == 0
现在我们可以定义一个函数divisibleBy 2
,它将检查任何参数该数字是否可以被二整除。
在这种情况下,isEven
函数如下所示:
isEven :: Int -> Bool
isEven = divisibleBy 2
关于haskell - 没有由文字 ‘0’ 产生的 (Num Bool) 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49052493/
我知道这是基本的,但我不确定,我哪里出错了,之前从未写过正则表达式.. 我需要从命令中获取一些输出 cmd | grep '[0-9]+:[0-9]+:[0-9]+:[0-9]+' 我需要grep一些
这个问题在这里已经有了答案: How does assignment work with list slices? [duplicate] (5 个答案) What does colon at as
我在一本 C 书中读到,对于数组 num[7],术语 num 等同于 &num[0]。这个概念对我来说很好,但是当我编写如下所示的程序时,我再次感到困惑。 #include #include int
例如,一种不好的方法是通过字符串分解: toReadableNum :: (Num a, Num b, Read b) => a -> b toReadableNum = read . show 如果
在输入文本字段中,我必须获取该值并在我的 .ts 文件中使用它。你能建议我应该使用以下哪种语法吗? 最佳答案 如果您希望在文本字段中看到的内容与变量中看到的内容之间进行同步对齐,请使用 如果您想在文
我正在研究 while 循环。我目前正在研究一个问题,要求从名为 a、b 和 c 的用户那里获取三个数字。我尝试显示 a 和 b 之间的所有数字,它们除以 c。我尝试使用“if”的想法,但没有成功。
因为这些符号,我很难用谷歌搜索。 num & 8, num >> 8 和 num >> 8 & 64 在 javascript 中是什么意思? 最佳答案 它们是 bitwise operators 关
显然,我的类型签名已关闭。从那以后我发现了原因。现在,我有兴趣了解更多关于我的错字上的 GHCI 推断签名。我试图让这段代码工作: elemNum :: (Eq a, Num b) => a -> [
我有一个程序根据定义的规则接受特定的字符串,即数字运算符编号。例如:2+4-5*9/8 上面的字符串是可以接受的。现在,当我输入类似 2+4-a 的内容时,它再次显示可接受,这是完全 Not Acce
我读到了这样一个旋转数组的解决方案 问题: public class Solution { public void rotate(int[] nums, int k) { in
一般来说,对于 int num , num++ (或 ++num ),作为读-修改-写操作,是 不是原子的 .但是我经常看到编译器,比如GCC ,为其生成以下代码( try here ): void
这个问题在这里已经有了答案: What's the difference between my ($variableName) and my $variableName in Perl? (4 个回答
如果列表中的 num 大于 0.5,我想得到 1,否则得到 0。 例如,a是一个列表 Matlab: b = (a > 0.5) Python: b = [1 if x > 0.5 else 0 fo
第4行为什么要在“-”后面加上“=”? num = 5 if num > 2: print(num) num -= 1 print(num) 最佳答案 num - 1:产生num减一的
iex> num = [9] '\t' 分配单个 [9] 列表返回 '\t'。这是什么原因? 最佳答案 您可以使用 i helper in IEx有关数据类型的更多信息: iex> i [9] Te
好吧,我做错了什么 - Moose 忽略了我的强制: package moo; use Moose; use Moose::Util::TypeConstraints; subtype Bar =>
这个问题已经有答案了: JavaScript property access: dot notation vs. brackets? (17 个回答) 已关闭 4 年前。 我正在练习编写一些代码。在练
我有声明 SELECT COUNT(*) as num_requests, ip_address FROM requests GROUP BY ip_address ORDER BY num_requ
有什么方法可以在 javascript 中编写更短的 (num>0)?num:0 吗? 原因是 num 变量还没有定义,在上面的例子中需要计算两次。 换句话说,a和b是已知的。我想写 (a>b)?(a
这个问题在这里已经有了答案: Why does 2 == [2] in JavaScript? (9 个回答) 关闭 9 年前。 这些陈述的计算结果为真是否有原因? 0 == [0]; 1 == [
我是一名优秀的程序员,十分优秀!