Haskell 由于使用简单函数使用“print”,导致类型变量“a0”不明确

Haskell 由于使用简单函数使用“print”,导致类型变量“a0”不明确,haskell,Haskell,我正在Haskell中实现一个函数: 我写过 f x k = h x k h x i = x^i/factorial i factorial n = foldl (*) 1 [1..n] 其中h是一个辅助函数,求和时将在f函数中使用。但现在,当我尝试使用该函数时,出现以下错误: <interactive>:109:1: error: • Ambiguous type variable ‘a0’ arising from a use of ‘print’ p

我正在Haskell中实现一个函数:

我写过

f x k = h x k

h x i = x^i/factorial i


factorial n = foldl (*) 1 [1..n]
其中h是一个辅助函数,求和时将在f函数中使用。但现在,当我尝试使用该函数时,出现以下错误:

<interactive>:109:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘print’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
        ...plus 22 others
        ...plus 19 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In a stmt of an interactive GHCi command: print it
如果我们检查h的类型,我们会看到:

例如,这给了我们:

Prelude> h 2 1
2.0
Prelude> h 2 2
2.0
Prelude> h 2 3
1.3333333333333333
如果我们检查h的类型,我们会看到:

例如,这给了我们:

Prelude> h 2 1
2.0
Prelude> h 2 2
2.0
Prelude> h 2 3
1.3333333333333333

这不是对您的问题的直接回答,但仍然是一个相关的观察结果:您的函数可以更高效地计算,并且当您像这样重写它时,它的实现将更有趣:


这不是对您的问题的直接回答,但仍然是一个相关的观察结果:您的函数可以更高效地计算,并且当您像这样重写它时,它的实现将更有趣:


您到底写了什么来查询系统?错误消息提到使用了打印,但您没有共享这样的代码。请共享导致错误的代码。我刚刚在GHCI中键入了H1。抱歉,不清楚。您到底写了什么来查询系统?错误消息提到使用打印,但您没有共享此类代码。请共享导致错误的代码。我刚刚在GHCI中键入了H1。抱歉说不清楚。
Prelude> h 2 1
2.0
Prelude> h 2 2
2.0
Prelude> h 2 3
1.3333333333333333