我想要一个show
(让我们称之为label
)的变体,它的行为就像show
,除了它不包含String
s in" "
或Char
s in ' '
。例子:
> label 5
"5"
> label "hello"
"hello"
> label 'c'
"c"
我尝试手动实现此功能,但遇到了一些问题。这是我尝试过的:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Label where
class (Show a) => Label a where
label :: a -> String
instance Label [Char] where
label str = str
instance Label Char where
label c = [c]
-- Default case
instance Show a => Label a where
label x = show x
但是,由于默认情况的类与 和 重叠instance Label [Char]
,因此instance Label Char
这些类型不适用于该label
函数。
是否有提供此功能的库函数?如果没有,是否有解决方法可以使上述代码正常工作?