将双精度列表格式化为字符串

时间:2013-05-02 14:02:28

标签: haskell map printf

我正在尝试将双打列表格式化为字符串。我想把一个列表如:

[0.38, 0.94]

成像:

"aString 0.380  0.940"

我试过了:

"aString" ++ map (printf "%7.3f") [0.38, 0.94]

并获得反馈:

<interactive>:80:19:
    No instance for (PrintfType Char)
      arising from a use of `printf'
    Possible fix: add an instance declaration for (PrintfType Char)
    In the first argument of `map', namely `(printf "%7.3f")'
    In the second argument of `(++)', namely
      `map (printf "%7.3f") [0.38, 0.94]'
    In the expression: "aString" ++ map (printf "%7.3f") [0.38, 0.94]

我无法解决这个问题。我无法撰写有效的签名。

谢谢

1 个答案:

答案 0 :(得分:12)

问题是map (printf "%7.3f") [0.38, 0.94]的类型为[String],而不是String。使用concatMap

Prelude Text.Printf> "aString" ++ concatMap (printf "%7.3f") [0.38, 0.94]
"aString  0.380  0.940"