- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于下面的代码:
module Main where
data EitherOr a b = Hello a | Goodbye b deriving Show
instance (Eq a, Eq b) => Eq (EitherOr a b) where
(==) (Hello x) (Hello x') = x == x'
(==) (Goodbye x) (Goodbye x') = x == x'
(==) _ _ = False
main :: IO ()
main = do
print (Hello 2 == Hello 2)
-- print (Hello 3 == Hello 2)
-- print (Goodbye 3 == Goodbye 3)
-- print (Goodbye 4 == Goodbye 3)
-- print (Hello 3 == Goodbye 3)
在runhaskell下执行,即在ghc下,出现如下错误:
• Ambiguous type variable ‘b0’ arising from a use of ‘==’
prevents the constraint ‘(Eq b0)’ from being solved.
Probable fix: use a type annotation to specify what ‘b0’ should be.
These potential instances exist:
instance Eq Ordering -- Defined in ‘GHC.Classes’
instance Eq Integer
-- Defined in ‘integer-gmp-1.0.2.0:GHC.Integer.Type’
instance (Eq a, Eq b) => Eq (EitherOr a b)
-- Defined at /tmp/runghcXXXX61964-0.hs:5:10. <-- This is because I am using org-mode source blocks
...plus 23 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘print’, namely ‘(Hello 2 == Hello 2)’
In a stmt of a 'do' block: print (Hello 2 == Hello 2)
In the expression: do print (Hello 2 == Hello 2)
|
12 | print (Hello 2 == Hello 2)
| ^^^^^^^^^^^^^^^^^^
我想我可以通过这样做给编译器一个类型提示
print ((Hello (2 :: Int)) == (Hello (2 :: Int)))
或类似的东西,但这似乎还不够。我看到 a 和 b 是多态的,但我认为在 main 中使用 == 可能足以帮助编译器推断类型。
接下来,我在 ghci 中加载了数据类型和类型类实例,并对类型进行了一些探索,例如发现
λ> :t Hello (2 :: Int)
Hello (2 :: Int) :: EitherOr Int b
正如预期的那样。再次在 ghci 中,我进行了更多探索,发现正在使用默认类型
λ> :t(你好 2 == 你好 2)
<interactive>:1:2: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Eq a0) arising from a use of ‘==’ at <interactive>:1:2-19
(Num a0) arising from the literal ‘2’ at <interactive>:1:8
• In the expression: (Hello 2 == Hello 2)
<interactive>:1:2: warning: [-Wtype-defaults]
• Defaulting the following constraint to type ‘()’
Eq b0 arising from a use of ‘==’
• In the expression: (Hello 2 == Hello 2)
(Hello 2 == Hello 2) :: Bool
这当然是我想要的。
然后我实际执行了 ghci 中的代码并通过一些默认设置得到了正确的答案
λ> Hello 2 == Hello 2
<interactive>:27:1: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Eq a0) arising from a use of ‘==’ at <interactive>:27:1-18
(Num a0) arising from the literal ‘2’ at <interactive>:27:7
• In the expression: Hello 2 == Hello 2
In an equation for ‘it’: it = Hello 2 == Hello 2
<interactive>:27:1: warning: [-Wtype-defaults]
• Defaulting the following constraint to type ‘()’
Eq b0 arising from a use of ‘==’
• In the expression: Hello 2 == Hello 2
In an equation for ‘it’: it = Hello 2 == Hello 2
<interactive>:27:1: warning: [-Wtype-defaults]
• Defaulting the following constraints to type ‘Integer’
(Eq a0) arising from a use of ‘==’ at <interactive>:27:1-18
(Num a0) arising from the literal ‘2’ at <interactive>:27:7
• In the expression: Hello 2 == Hello 2
In an equation for ‘it’: it = Hello 2 == Hello 2
<interactive>:27:1: warning: [-Wtype-defaults]
• Defaulting the following constraint to type ‘()’
Eq b0 arising from a use of ‘==’
• In the expression: Hello 2 == Hello 2
In an equation for ‘it’: it = Hello 2 == Hello 2
True
但是在 runhaskell 下执行的相同代码,即在 ghc 编译下,失败并出现我首先给出的错误。我需要在这里学习什么?
最佳答案
GHCi 中的类型默认规则与编译 GHC 等程序时使用的规则不同。当类型不明确时,您应该给出明确的签名,例如:
print (Hello 2 == (Hello 2 :: EitherOr Integer ())
在实践中,这并不经常需要,因为程序的其他部分隐含了类型。像上面这样的玩具和教育片段没有太多可以为类型检查器添加信息的上下文。
关于haskell - 第 6 章,练习 7,Haskell 从第一原则开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57948829/
K&R 前。 4.2 要求您修改给定的(非标准)atof 函数,该函数缺少处理指数的指数处理机制(如 123e6 或 456e-7)。我添加了一个最小的更改来处理正确输入的、无空格的个位数指数。为了检
我正在学习计算机科学入门类(class)的考试,我对“常规”算法和递归算法中的复杂性主题有疑问(通常我们将这些问题写成 C 代码). 我想知道 Internet 和/或书籍中是否有涵盖该主题的基础级别
console.log( ‘blah’.repeatMe( 3 ) ); 使用 Javascript 编写代码,使前面的函数打印: 输出:blahblahblah 最佳答案 噢,放弃函数式解决方案太有
我正在准备 Java SE 7 认证考试,并且正在做一些关于继承和访问修饰符的无聊练习。 但是现在我在应用继承时遇到了意外的行为。在我的基础包 com.testpkg 中,我有一个抽象类: packa
我刚刚开始了 C 语言队列的第一课,我得到了创建队列、向队列添加元素和删除元素的练习。但是,我在检查队列是满还是空时遇到了麻烦。 #include typedef struct FloatQueue
请问我从昨天开始就被困在下面这个问题中了。下面是问题: Write a program that uses console.log to print all the numbers from 1 to
我最近尝试了一些 Java,希望对我的风格进行一些评论。如果你喜欢看这个放在图像中的练习,并告诉我我的风格是否足够好?或者是做的还不够好,可以告诉我应该在哪方面多下工夫,帮我改进一下? exercis
我对手动编写 SQL 查询还很陌生,而且我有一个我似乎无法解决的练习。 我了解解决此问题所需的工具,但我就是想不出解决方案。 你能帮助我理解如何以一种能让我在未来解决类似练习的方式解决这个问题吗? 我
好吧,这就是练习: Define a class named student, containing three grades of students. The class will have a f
我是一个 JS 菜鸟,试图制作这个“你好,先生/小姐 你的名字!”干净的。我看不到在 if/else 中重构警报的方法,因为那样我就失去了 var b 的值。 JS: "use strict
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
反转二维数组的值,可以扩展 n 次。 [1, [2, [3, ... [n, null]]]] 给定: 所有数组的长度始终为 2 列表中的最后一个数组将包含一个 null 索引 1 示例: [1, [
我试图通过 Jason Hickey 笔记自学 OCaml,下面的练习让我难住了。 问题:编写一个函数 sum 给定两个整数边界 m,n 和函数 f 计算求和。 我正在尝试这个: let r
这是一个生成斐波那契数列的程序,这里是引用:http://sicp.org.ua/sicp/Exercise1-19 据说我们可以将程序视为“a <- bq + aq + ap and b <- bp
所以,我正在努力通过 SICP。 第 4 章的第一个练习是: Exercise 4.1. Notice that we cannot tell whether the metacircular eva
这个问题已经有答案了: Count the number of occurrences of a character in a string in Javascript (39 个回答) 已关闭 6
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我目前正在学习 JS,并且正在尝试进行编码练习。到目前为止,我已经成功地使用离线和在线部分代码的大量资源拼凑了以下代码。我已经非常接近了 - 只是结果中的数字无法正确。 一些背景:在函数中输入一个对象
我需要创建一个回收器 View 练习,这是一个带有简单的单个回收器的应用程序加载大小为 20 的页面,并且可以容纳无限数量的项目。 现在我不想做出重新加载越来越多的项目的幼稚解决方案,而是一个优雅的解
下面的实现正确吗? 输入:Oldrecords(GameRecord 对象数组)和 newRecords (GameRecord) 我将检查 oldRecords 数组中的 newRecord 值。如
我是一名优秀的程序员,十分优秀!