开发者

About ++ to concate things

Given the followin开发者_JAVA技巧g function signature:

concatThings :: (Show a) => a -> String -> String
concatThings any str2 = str2 ++ (show any)

If run concatThings "there" "hi", then result will be: "hi\"there\"", but what I want is just "hithere".

How can I still get "hithere" with this function signature?


With this function signature, you can't. You must either get rid of the show, reducing the function to

concatStrs :: String -> String -> String
concatStrs = flip (++)

or introduce a new type class to replace Show.


You could wrap the string in a new type and provide a simple Show instance for it:

newtype PartialString = PartialString String

instance Show PartialString where
    show PartialString str = str

and then pass in a wrapped string to the concatThings function:

let pstr1 = PartialString str1 in concatThings pstr1 str2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜