0
import Text.Printf
printf "the length of this list is %d"  length' [1,2,3,4,5]

私はこれを行いますが、失敗しました。

'func.hs:38:58:
    No instance for (Num t0) arising from the literal `1'
    The type variable `t0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float'
      instance Num Float -- Defined in `GHC.Float'
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
      ...plus three others
    In the expression: 1
    In the third argument of `printf', namely `[1, 2, 3, 4, ....]'
    In a stmt of a 'do' block:
      printf "the length of this list is %d" length' [1, 2, 3, 4, ....]"
4

2 に答える 2

3

最初に修正する必要があるのは、あなたがprintf'思っているように 2 つではなく、3 つの引数に適用していることです。length'アプリケーションを括弧で明示的に囲みます。

printf "string" (length' [1, 2, 3, 4])
printf "string" $ length' [1, 2, 3, 4] -- The more idiomatic version of the above.

正気の型を持っている場合length'(これに似ているlengthgenericLength、これよりも型エラーが解消されます。

于 2013-09-23T03:33:02.040 に答える
0

またはのいずれputStrLnputStr。タイプが、扱うほとんどのタイプのタイプクラスであるshow場合にのみ使用するか、独自のインスタンスを作成できます。Show

putStr $ show $ length [1..5]
于 2013-09-23T03:25:36.023 に答える