Haskell中是否有漂亮的打印(或显示)抽象? [英] Is there a pretty printing (or showing) abstraction in Haskell?

查看:109
本文介绍了Haskell中是否有漂亮的打印(或显示)抽象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否没有标准的方式来打印出最终用户消费的价值? Show 显然更适合于调试的便利性,因为它的使用约定和 read(显示x )== x



例如,至少没有像


$ b这样的简单包$ b

  class(Show a)=> PShow a where 
pshow :: a - > String
pshow = show

pprint ::(PShow a)=> a - > IO()
pprint = putStrLn。 pshow

其中实例的功能类似于

 实例PShow MyType其中
pshow a = someUserFriendlyStringOf a

请注意,我没有要求提供详尽的漂亮的打印和格式化功能(我看到几个包可以做到这一点),只是为了一个简单的抽象,它被广泛使用,允许漂亮的打印。有没有这样的事情?

解决方案

我刚做了一个 Hoogle 搜索 a - > String ,找到 Language.Haskell.Pretty 模块。



模块定义了三个简单函数:


prettyPrintStyleMode :: Pretty a =>样式 - > PPHsMode - > a - >字符串

使用给定样式和模式的漂亮打印。 p>

prettyPrintWithMode :: Pretty a => PPHsMode - > a - > String

使用默认样式和a



prettyPrint ::漂亮a => a - >字符串
使用默认样式漂亮地打印和defaultMode。

整个类型类似于你所要求的更复杂,但也许它有帮助


Is there no standard way to print out values for end-user consumption? Show is clearly more of a debugging convenience than something that would work for this purpose, given the conventions around its use and the constraint that read (show x) == x.

For example, isn't there at least a simple package like

class (Show a) => PShow a where 
    pshow :: a -> String
    pshow = show

pprint :: (PShow a) => a -> IO () 
pprint = putStrLn . pshow

where instances do something like

instance PShow MyType where
    pshow a = someUserFriendlyStringOf a

Note that I'm not asking for something that provides elaborate pretty printing and formatting functionality (I see several packages that do that) just for a simple abstraction that's widely used that allows for pretty printing. Is there something like this?

解决方案

I just did a Hoogle search for a -> String and found the Language.Haskell.Pretty module.

The module defines three simple functions:

prettyPrintStyleMode :: Pretty a => Style -> PPHsMode -> a -> String
pretty-print with a given style and mode.

prettyPrintWithMode :: Pretty a => PPHsMode -> a -> String
pretty-print with the default style and a given mode.

prettyPrint :: Pretty a => a -> String
pretty-print with the default style and defaultMode.

The whole typeclass seems to be a bit more complex than what you have asked for, but maybe it helps

这篇关于Haskell中是否有漂亮的打印(或显示)抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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