user3025403
user3025403

Reputation: 1080

Haskell printing strings stdout

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

Upvotes: 1

Views: 1824

Answers (1)

krzysz00
krzysz00

Reputation: 2103

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.

Upvotes: 6

Related Questions