以不同的方式显示Haskell列表? [英] display a list differently Haskell?

查看:93
本文介绍了以不同的方式显示Haskell列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [one,two, 3] 

显示为

one,two,three

需要它为文件完成



感谢

解决方案

你可以做插入来自 Data.List

  showList :: Show a => [a]  - >字符串
showList =插入,。 map show

map show 将每个元素( intercalate,)插入逗号和空格之间的部分并将它们粘合在一起。

使用引号(和任何内部引号正确转义) b $ b

hey i was wandering if it was possible to show a list:

["one", "two", "three"]

to be shown as

"one", "two", "three"

need it done for a file

thanks

解决方案

You can do this with intercalate from Data.List

 showList :: Show a => [a] -> String
 showList = intercalate ", " . map show

The map show converts each element to it's string representation with quotes (and any internal quotes properly escaped), while intercalate ", " inserts commas and spaces between the pieces and glues them together.

这篇关于以不同的方式显示Haskell列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆