开发者

Haskell non-digit string list to a different list [closed]

开发者 https://www.devze.com 2023-02-25 16:33 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have a list ['%','&','/']. How can i convert it into the form [%,&开发者_如何学Python,/] ?


myShow :: [Char] -> String
myShow s = concat ["[", intersperse ',' s, "]"]

Use it like this:

putStrLn (myShow ['%','&','/'])     -- prints [%,&,/]

But if you want this to work with show and print, you will have to define your own type:

data MyChar = MyChar Char

instance Show MyChar
  where show (MyChar ch) = [ch]

And then operate on [MyChar] rather than [Char]:

let myList = map MyChar ['%','&','/']
-- ... do whatever you want with myList ...
print myList                        -- prints [%,&,/]


"[" ++ intercalate ',' list ++ "]"

intercalate is declared in Data.List.

0

精彩评论

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