Haskell打印字符串stdout [英] Haskell printing strings stdout

查看:237
本文介绍了Haskell打印字符串stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在powerset中打印元素。现在我的代码的输出是这样的:

 a
ab
b
x
xy
xyz
xz
y
yz
z

但是,我需要输出不带引号,如下所示:

  a 
ab
b
x
xy
xyz
xz
y
yz
z

这就是我所拥有的。

  import Data.List 
powerset = foldr(\ x acc - > acc ++ map(x :) acc)[[]]

main = do
numCases< - getLine
repl $(读取numCases :: Int)

repl num = do
if(num == 0)then return()
else do
size< - getLine
input< ; - getLine
let ret = tail $ sort $ powerset input $ b $ mapM(\ x - > print x)ret
repl $ num-1


解决方案

第一(\x - > fx)相当于普通的 f (在几乎所有情况下)通过eta-reduction。因此,您可以将 mapM(\ x - > print x)重写为 mapM print 。 p>

要删除引号,您应该使用函数 putStrLn 而不是 print 函数。 print 中的引号来自 print = putStrLn。显示 show 是一个函数,它以一种可以(如果定义了一个合适的实例)的方式打印出值,并用读入。因此,字符串上的引号,您不希望(或需要)用于您的用例。


I need to print the elements in a powerset. Right now my code's output is this:

"a"
"ab"
"b"
"x"
"xy"
"xyz"
"xz"
"y"
"yz"
"z"

However, I need the output to not have quotation marks, like this:

a
ab
b
x
xy
xyz
xz
y
yz
z

This is what I have. How do I fix it do to get the right output?

import Data.List
powerset = foldr (\x acc -> acc ++ map (x:) acc) [[]]

main = do
    numCases <- getLine
    repl $ (read numCases :: Int)

repl num = do
    if(num == 0) then return ()
    else do
        size <- getLine
        input <- getLine
        let ret = tail $ sort $ powerset input
        mapM (\x -> print x) ret
        repl $ num-1

解决方案

First (\x -> f x) is equivalent to plain f (in almost all cases) by eta-reduction. So, you can re-write mapM (\x -> print x) as mapM print.

To remove the quotation marks, you should use the function putStrLn instead of the print function. The quotation marks in print come from print = putStrLn . show. show is a function that prints out values in a way that can (if a suitable instance is defined) be read back in with read. Thus, quotation marks on strings, which you don't want (or need) for your use-case.

这篇关于Haskell打印字符串stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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