Haskell:显示时抑制引号 [英] Haskell: Suppress quotes around strings when shown

查看:127
本文介绍了Haskell:显示时抑制引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码

 数据HelloWorld = HelloWorld; 
实例显示HelloWorld其中show _ =hello world;

hello_world =hello world

main = putStr $ show $(HelloWorld,hello_world)

打印:

 (hello world,hello world)

我想要打印:

 (hello world,hello world)

即我希望以下行为:

  fhello world=hello world
f HelloWorld =hello world

不幸的是, show 不符合这个要求,为:

  showhello world=\hello world \

是否有一个像上面描述的 f 的函数?

解决方案

首先,看看这个问题。也许你会对 toString 函数感到满意。其次, show 是一个将某些值映射到 String 的函数。



所以,引用应该是有意义的:

  >显示字符串
\字符串\




是否有一个像上面描述的 f 的函数?

好像您正在寻找 id

  > putStrLn $ idstring
string
> putStrLn $ showstring
string


The following code:

data HelloWorld = HelloWorld; 
instance Show HelloWorld where show _ = "hello world";

hello_world = "hello world"

main = putStr $ show $ (HelloWorld, hello_world)

Prints:

(hello world,"hello world")

I'd like it to print:

(hello world,hello world)

i.e. I want behaviour like the following:

f "hello world" = "hello world"
f HelloWorld = "hello world"

Unfortunately, show does not satisfy this, as:

show "hello world" = "\"hello world\""

Is there a function that works like f that I've described above?

解决方案

Firstly, take a look at this question. Maybe you will be satisfied with toString function.

Secondly, show is a function that maps some value to a String.

So, it makes sense that quotes should be escaped:

> show "string"
"\"string\""

Is there a function that works like f that I've described above?

Seems like you're looking for id:

> putStrLn $ id "string"
string
> putStrLn $ show "string"
"string"

这篇关于Haskell:显示时抑制引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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