gpt4 book ai didi

Haskell 类型错误 : Could not deduce (Show a) arising from a use of `show' from the context (Num a)

转载 作者:行者123 更新时间:2023-12-02 21:11:52 25 4
gpt4 key购买 nike

我正在配置 xmonad,由于我必须启动几个 dzen 实例,因此我决定最好使用一个接受 x 和 y 位置、宽度、高度和文本对齐参数的函数:

-- mydzen.hs

import Data.List

-- | Return a string that launches dzen with the given configuration.
myDzen :: Num a => a -> a -> a -> Char -> String -> String

myDzen y x w ta e =
intercalate " "
[ "dzen2"
, "-x" , show x
, "-w" , show w
, "-y" , show y
, "-h" , myHeight
, "-fn" , quote myFont
, "-bg" , quote myDBGColor
, "-fg" , quote myFFGColor
, "-ta" , [ta]
, "-e" , quote e
]

quote :: String -> String
quote x = "'" x "'"

-- dummy values
myHeigth = "20"
myFont = "bitstream"
myDBGColor = "#ffffff"
myFFGColor = "#000000"

Could not deduce (Show a) arising from a use of `show'
from the context (Num a)
bound by the type signature for
myDzen :: Num a => a -> a -> a -> Char -> String -> String
at mydzen.hs:(5,1)-(17,13)
Possible fix:
add (Show a) to the context of
the type signature for
myDzen :: Num a => a -> a -> a -> Char -> String -> String
In the expression: show x
In the second argument of `intercalate', namely
`["dzen2", "-x", show x, "-w", ....]'
In the expression:
intercalate " " ["dzen2", "-x", show x, "-w", ....]

显然,删除签名,或者更改Num a对于 Show a解决了问题,但我不明白为什么。 “论点”x , wy 应该几乎是任何类型的数字( 100550.21366 * 0.7 ecc)。

我是 Haskell 新手,直到现在我还无法(清楚地)理解错误或找出问题所在。

最佳答案

以前,ShowEqNum 的父类(super class),并且代码可以编译。

在新的GHC中,从7.4版本开始,这已经改变了,现在Num不依赖于ShowEq,所以你需要将它们添加到签名中。

原因是关注点分离 - 有些数字类型没有合理的相等性和显示函数(可计算实数、函数环)。

关于Haskell 类型错误 : Could not deduce (Show a) arising from a use of `show' from the context (Num a),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11193219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com