- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在ghci中加载以下代码
{-# OPTIONS_GHC -Wall #-}
--following gives a type error about the arg int not str
bug :: String -> Int -> Int
bug str int = num
where num = str * 10 + int
-- the following gives a type error about the arg str as expected
works :: String -> Int -> Int
works str int = str * 10 + int
出现以下错误:
:reload
[1 of 1] Compiling Main ( bugTI.hs, interpreted )
bugTI.hs:6:16: error:
• Couldn't match type ‘[Char]’ with ‘Int’
Expected: Int
Actual: String
• In the expression: num
In an equation for ‘bug’:
bug str int
= num
where
num = str * 10 + int
|
6 | bug str int = num
| ^^^
bugTI.hs:7:29: error:
• Couldn't match type ‘Int’ with ‘[Char]’
Expected: String
Actual: Int
• In the second argument of ‘(+)’, namely ‘int’
In the expression: str * 10 + int
In an equation for ‘num’: num = str * 10 + int
|
7 | where num = str * 10 + int
| ^^^
bugTI.hs:10:17: error:
• Couldn't match type ‘[Char]’ with ‘Int’
Expected: Int
Actual: String
• In the first argument of ‘(*)’, namely ‘str’
In the first argument of ‘(+)’, namely ‘str * 10’
In the expression: str * 10 + int
|
10 | works str int = str * 10 + int
| ^^^
Failed, no modules loaded.
函数错误中第 7 行的错误表明它期望 int 是一个字符串,这在算术表达式中是没有意义的。
函数中第 10 行的相应错误表示它期望 str 是一个有意义的 Int。
在我看来,第 7 行的错误充其量是误导性的,最坏的情况下(而且不太可能)是一个错误。有人可以解释为什么错误指向错误的变量吗?我想有些事情我不明白。
最佳答案
It seems to me that the error for line 7 is at best misleading, at worst (and unlikely) a bug. Can somebody explain why the error points to the wrong variable? I assume there is something I don't understand.
(*) :: Num a => a -> a -> a
没有任何“特别”之处, (+) :: Num a => a -> a -> a
等功能。这些只是在 Num
中定义的函数类型类。
您可以使其他类型成为 Num
的成员typeclass 也是如此,如果人们定义自己的类似数字的类型,这通常会发生。
因此,如果您愿意,您可以为 Num
定义一个实例对于 String
类型,或一般列表。例如,您可以根据列表的长度对数字进行编码,从而推导出其算术。那看起来像:
import Data.List(genericReplicate)
instance Num [a] where
(+) = (++)
(*) = (<*)
abs = id
signum = take 1
fromInteger = (`genericReplicate` undefined)
xs - ys = drop (length ys) xs
我同意这作为 Num
的实例没有多大意义, 但它是实现这一点的可能方法之一。
Haskell 提示的是两个操作数和返回类型都需要具有相同的类型。确实,在签名中 Num a => <b>a</b> -> <b>a</b> -> <b>a</b>
, a
用于两个操作数和返回类型。
自 int
类型为 Int
通过类型签名,这意味着加法的两个操作数需要是 Int
s,因此是 str * 10
的结果应该是 Int
, 这对 10
不是问题, 但是 str
的问题.即使Num [a]
会成立,这仍然意味着这会出错,因为你不能乘以 Int
用Double
例如,或添加 Integer
和 Int
在一起。
关于Haskell 类型推断导致错误消息指向错误的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73680760/
有人能给我解释一下我下面程序中的一点吗我编写的以下程序是为了理解 int * const * var 的含义; #include "iostream" using namespace std ; in
我正在摆弄 C 中的指针,但仍然不确定一些非常基础的知识。我想出了以下示例代码: #include int main(void) { int num = 42; /
以下代码产生警告: const char * mystr = "\r\nHello"; void send_str(char * str); void main(void){ send_str
我正在尝试在我的 Linux 计算机上升级 cmake。这是我使用的命令。 删除旧的 cmake: sudo apt purge --auto-remove cmake 从 https://cmake
我试图借助 char 指针来追踪 union 第一个字节的值。我假设 byte1 的输出应该是 ff,但实际上是 ffffffff。如果我错了请纠正我。 #include #include #in
我正在使用微 Controller 进行一些 ADC 测量。当我尝试使用 -O2 优化编译以下代码时遇到问题,当代码中存在 PrintVal() 函数时,MCU 卡住。我做了一些调试,结果发现,当我添
#include #include using namespace std; int main () { vector qwerty; qwerty.push_back(5);
我有我的 woking setup.py 文件;其中包含以下行: home = os.environ["HOME"] # home = /home/dr in my machine; distu
这段代码 #include #include static_assert(std::is_same_v::value_type, volatile int>); 在最新的 GCC 和 clang
我对 C 中的前向声明有疑问。 代码 typedef struct yhash_s t_yhash;// forward declaration struct yhash_s { size_t
我想提取成员指针指向的成员的类型。 template void demo(myClass& instance, void* ptr) { instance.*member = *reinter
我正在尝试使用指针将一段 C 代码转换为 Swift 3。这是 C 代码中的相关部分。 Float32 sampleArray[256] = { // Array is 256 Float value
您可能知道,VLA's haves pros and cons 和它们在 C11 中是可选的。 我想使 VLA 成为可选项的主要原因是:“堆栈可能会爆炸”: int arr[n]; /* where
这段代码有什么错误?为什么我不能按照我尝试的方式取消引用该元素。 #include typedef struct { int value; struct node * left;
有什么方法可以在 JavaScript 中创建\返回指向变量的指针吗? 比如,在 PHP 中: function func() { ..... return &$result; } 我
如果您想使用方法的指针作为参数,则需要将该方法键入作为对象的函数,就像这样好 : type TAcceptor = function(filename:string):boolean of objec
很简单的问题: 我对 C++ 中的智能指针有点陌生。我想我得到了所有权的东西,但我不知道如何访问他们实际指向的内容。当我尝试使用对象的成员函数/变量时,我只是得到了 unique_ptr 类的函数,这
我得到了一个点的方位 Angular 、指南针方向和一个可以将箭头设置到某个方向的 api(0 是顶部,90 是右侧,180 是底部,360 是顶部) 如果我希望箭头指向我采用方位 Angular 形
我正在尝试找到一种方法,从单元格中获取位于当前工作表左侧(托盘下方)的工作表单元格中的数据。 我知道如何通过调用其他工作表 =Sheet1!A1 但现在我需要一些最好的解释 =Sheet[-1]!A1
所以我在 MATLAB 中有一个 for 循环,其中向量 x 将通过一个函数,比如 cos(x).^2,或者不同的选择,比如 sin(x).^2 + 9.*x。用户将在 for 循环之前选择他想使用的
我是一名优秀的程序员,十分优秀!