Showing a Haskell list of tuples with custom syntax

点点圈 提交于 2019-12-20 04:22:47

问题


I have a list of tuples [(1,'a','%',"yes"),(2,'b','[',"no"),(3,'c',']',"ok")]. How can I show this list as output in the form of [(1,a,%,yes),(2,b,[,no),(3,c,],ok)]?


回答1:


Looks like the transformation you wish to make is to strip out quote characters? If so, filtering the results of calling show on your data will be enough:

 > let x = [(1,'a','%',"yes"),(2,'b','[',"no"),(3,'c',']',"ok")]

Then apply a filter,

 > putStrLn . filter (`notElem` "'\"") . show $ x
 [(1,a,%,yes),(2,b,[,no),(3,c,],ok)]

Once you know that show turns a data structure into a pretty string, processing that string to make minor modifications is pretty easy.



来源:https://stackoverflow.com/questions/5683210/showing-a-haskell-list-of-tuples-with-custom-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!