haskell 另一个函数作为输入参数
haskell another function as input parameter
hi_copy.hs
waxOn = x
where
z = 7
y = z + 18
k = y ^ 2
x = k * 5
triple = a
where
a = a * 3
然后加载。
ghci> :load hi_copy.hs
[1 of 1] Compiling Main ( hi_copy.hs, interpreted )
Ok, one module loaded.
然后 运行
triple waxOn
<interactive>:122:1: error:
• Couldn't match expected type ‘Integer -> t’
with actual type ‘Integer’
• The function ‘triple’ is applied to one value argument,
but its type ‘Integer’ has none
In the expression: triple waxOn
In an equation for ‘it’: it = triple waxOn
• Relevant bindings include it :: t (bound at <interactive>:122:1)
运行 3 * waxOn
会起作用。
但现在我不知道如何让 triple waxOn
工作。
meta:到现在还没有学习 haskell 中的类型。也许已经存在其他好的答案。但是我看不懂别人的好答案。
您的 triple
没有接受任何参数。它只有 returns 一个值,这个值将是无限递归的结果,其中 a
是 ((((…) * 3) * 3) * 3)
。
您应该将参数作为输入,因此:
triple :: Num a => a -> a
triple a = a * 3
或更短:
triple :: Num a => a -> a
triple = (3 *)
hi_copy.hs
waxOn = x
where
z = 7
y = z + 18
k = y ^ 2
x = k * 5
triple = a
where
a = a * 3
然后加载。
ghci> :load hi_copy.hs
[1 of 1] Compiling Main ( hi_copy.hs, interpreted )
Ok, one module loaded.
然后 运行
triple waxOn
<interactive>:122:1: error:
• Couldn't match expected type ‘Integer -> t’
with actual type ‘Integer’
• The function ‘triple’ is applied to one value argument,
but its type ‘Integer’ has none
In the expression: triple waxOn
In an equation for ‘it’: it = triple waxOn
• Relevant bindings include it :: t (bound at <interactive>:122:1)
运行 3 * waxOn
会起作用。
但现在我不知道如何让 triple waxOn
工作。
meta:到现在还没有学习 haskell 中的类型。也许已经存在其他好的答案。但是我看不懂别人的好答案。
您的 triple
没有接受任何参数。它只有 returns 一个值,这个值将是无限递归的结果,其中 a
是 ((((…) * 3) * 3) * 3)
。
您应该将参数作为输入,因此:
triple :: Num a => a -> a
triple a = a * 3
或更短:
triple :: Num a => a -> a
triple = (3 *)