开发者

About ++ to concate things

开发者 https://www.devze.com 2023-03-22 14:13 出处:网络
Given the followin开发者_JAVA技巧g function signature: concatThings :: (Show a) => a -> String -> String

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

精彩评论

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