Given the code below:
import Data.List;
main = (readLn :: IO [Integer]) >>= print . subsequences
It takes a list of integers from standard input (for example [1,2,3]
) and outputs something like:
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
I want it to be like this:
{},{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3}}
so my goal is to replace every [
and ]
in the result string with {
and }
respectively.
How can I achieve this?
Best How To :
All you need is love and to split print
into putStrLn . show
and then add a simple map
in-between which does the conversion:
main :: IO ()
main =
let fn '[' = '{'
fn ']' = '}'
fn c = c
in (readLn :: IO [Integer]) >>= putStrLn . map fn . show . subsequences
Live demo
The above code, given [1, 2, 3]
, will output:
{},{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3}}
If this is a code golf, then you could have (106 characters):
f '[' = '{';f ']' = '}';f c = c;main = (readLn :: IO [Integer]) >>= putStrLn . map f . show . subsequences
Live demo