Printing a list of lists without parentheses

I try to print Pascal's triangle to some arbitrary line after I 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 well besides printing. When I use pascal 4 , I get:

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

When I really want this:

 1 1 1 1 2 1 1 3 3 1 

Can I do this?

+6
source share
2 answers

Define your own print function:

 import Data.List (intercalate) show' :: Show a => [a] -> String show' = intercalate " " . map show 
+13
source

You can use unwords / unlines:

 import Data.List ... putStr $ unlines $ map (unwords . map show) $ pascal n 
+6
source

Source: https://habr.com/ru/post/955521/


All Articles