Haskell: 无法匹配预期的类型
Haskell: Couldn't match expected type
我正在尝试学习 Haskell 并且我试图理解的一小部分示例代码正在编译,但是在应用代码时我 运行 进入“无法匹配预期类型”错误。
谁能给我一些关于我在做什么的指导wrong/how我应该这样做吗?
这段代码是在讲座中给出的:
> mult :: Integer -> (Integer, Integer-> Integer )
> mult x = (x,(*x))
按照我在讲座中被告知的那样执行snd (mult 3) 5
15
但是在执行 (mult 3) 5
时出现以下错误
<interactive>:133:1: error:
• Couldn't match expected type ‘Integer -> t’
with actual type ‘(Integer, Integer -> Integer)’
• The function ‘mult’ is applied to two arguments,
but its type ‘Integer -> (Integer, Integer -> Integer)’
has only one
In the expression: (mult 3) 5
In an equation for ‘it’: it = (mult 3) 5
• Relevant bindings include it :: t (bound at <interactive>:133:1)
看到错误,我尝试使用一个参数 mult 3
应用 mult
,这导致了以下错误
<interactive>:135:1: error:
• No instance for (Show (Integer -> Integer))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
为什么我不能在没有 snd
功能的情况下使用 mult
?
(Integer, Integer -> Integer)
这不是函数。它可能看起来有点像两个参数的函数,但它实际上被括号括起来为(Integer, (Integer -> Integer))
所以它是一个元组,其中第一件事只是一个普通数字,第二件事是一个函数。
mult :: Integer -> (Integer, Integer -> Integer)
mult x = (x, (*x))
在这里,我们取一个数字 x
和 return 一个二元组。这个元组的第一个元素就是数字 x
,仅此而已。第二个元素是乘以 x
的函数。这是两个不同的值,没有以任何方式联系在一起。
fst (mult 3)
这是元组的第一个元素,它只是一个数字。在这种情况下,我们将得到 3
.
snd (mult 3)
这是元组的第二个元素,一个从Integer
到Integer
的函数。我们无法打印出 Haskell 中的函数(因为它们没有实现 Show
),因此我们必须将其应用于参数才能获得结果。
snd (mult 3) 5 -- Or, equivalently, (snd (mult 3)) 5
这将函数“乘以 3”应用于数字 5,所以我们得到 5 乘以 3,或 15
。
(mult 3) 5
这需要一个元组 mult 3
(其值为 (x, (* x))
),并尝试将其应用于数字。在 Haskell、 中,只有 函数可以被赋予参数,元组不是函数,所以你会得到一个错误,基本上说“我希望这里有一个函数,但我得到了一个元组。
Why can't I use mult
without the snd
function?
因为 mult 5
returns 一个二元组,它 returns (5, (* 5))
,所以第一项是 Integer
而第二项是一个函数类型为 Integer -> Integer
.
当您对它应用 3
时,您不能将此二元组用作函数。 (5, (* 5)) 3
的结果是什么?通过使用 snd (5, (* 5))
它 returns (* 5)
,然后你可以将它用于 (* 5) 3
,即 15
.
此外,您不能将 show f
与 f
函数一起使用,因为对于通用函数,它无法产生输出。因此无法打印 mult 3
的结果,您可以打印 fst (mult 3)
例如,即 3
.
我正在尝试学习 Haskell 并且我试图理解的一小部分示例代码正在编译,但是在应用代码时我 运行 进入“无法匹配预期类型”错误。 谁能给我一些关于我在做什么的指导wrong/how我应该这样做吗?
这段代码是在讲座中给出的:
> mult :: Integer -> (Integer, Integer-> Integer )
> mult x = (x,(*x))
按照我在讲座中被告知的那样执行snd (mult 3) 5
15
但是在执行 (mult 3) 5
时出现以下错误
<interactive>:133:1: error:
• Couldn't match expected type ‘Integer -> t’
with actual type ‘(Integer, Integer -> Integer)’
• The function ‘mult’ is applied to two arguments,
but its type ‘Integer -> (Integer, Integer -> Integer)’
has only one
In the expression: (mult 3) 5
In an equation for ‘it’: it = (mult 3) 5
• Relevant bindings include it :: t (bound at <interactive>:133:1)
看到错误,我尝试使用一个参数 mult 3
应用 mult
,这导致了以下错误
<interactive>:135:1: error:
• No instance for (Show (Integer -> Integer))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
为什么我不能在没有 snd
功能的情况下使用 mult
?
(Integer, Integer -> Integer)
这不是函数。它可能看起来有点像两个参数的函数,但它实际上被括号括起来为(Integer, (Integer -> Integer))
所以它是一个元组,其中第一件事只是一个普通数字,第二件事是一个函数。
mult :: Integer -> (Integer, Integer -> Integer)
mult x = (x, (*x))
在这里,我们取一个数字 x
和 return 一个二元组。这个元组的第一个元素就是数字 x
,仅此而已。第二个元素是乘以 x
的函数。这是两个不同的值,没有以任何方式联系在一起。
fst (mult 3)
这是元组的第一个元素,它只是一个数字。在这种情况下,我们将得到 3
.
snd (mult 3)
这是元组的第二个元素,一个从Integer
到Integer
的函数。我们无法打印出 Haskell 中的函数(因为它们没有实现 Show
),因此我们必须将其应用于参数才能获得结果。
snd (mult 3) 5 -- Or, equivalently, (snd (mult 3)) 5
这将函数“乘以 3”应用于数字 5,所以我们得到 5 乘以 3,或 15
。
(mult 3) 5
这需要一个元组 mult 3
(其值为 (x, (* x))
),并尝试将其应用于数字。在 Haskell、 中,只有 函数可以被赋予参数,元组不是函数,所以你会得到一个错误,基本上说“我希望这里有一个函数,但我得到了一个元组。
Why can't I use
mult
without thesnd
function?
因为 mult 5
returns 一个二元组,它 returns (5, (* 5))
,所以第一项是 Integer
而第二项是一个函数类型为 Integer -> Integer
.
当您对它应用 3
时,您不能将此二元组用作函数。 (5, (* 5)) 3
的结果是什么?通过使用 snd (5, (* 5))
它 returns (* 5)
,然后你可以将它用于 (* 5) 3
,即 15
.
此外,您不能将 show f
与 f
函数一起使用,因为对于通用函数,它无法产生输出。因此无法打印 mult 3
的结果,您可以打印 fst (mult 3)
例如,即 3
.