Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print List of Lists without brackets

Tags:

haskell

I am trying to print Pascals triangle up to some arbitrary row, after some thought I came up with this solution:

next xs = zipWith (+) ([0] ++ xs) (xs ++ [0])
pascal n = take n (iterate next [1])

main = do
   n <- readLn :: IO Int
   mapM_ putStrLn $ map show $ pascal n

Which works quite well, except for the printing. When I apply pascal 4 I get:

[1]
[1,1]
[1,2,1]
[1,3,3,1]

When what I really want is this:

1
1 1
1 2 1
1 3 3 1

Is there any way I can do this?

like image 340
Bob Templ Avatar asked Oct 08 '13 19:10

Bob Templ


2 Answers

Define your own pretty-printing function:

import Data.List (intercalate)

show' :: Show a => [a] -> String
show' = intercalate " " . map show
like image 147
Fixnum Avatar answered Oct 04 '22 09:10

Fixnum


You could unwords / unlines:

import Data.List
...
putStr $ unlines $ map (unwords . map show) $ pascal n
like image 25
Sassa NF Avatar answered Oct 04 '22 09:10

Sassa NF