I have this self-defined IO() Triangle
, which has each row of a triangle as an element in a list:
import Data.List ( intersperse )
type Triangle = Char -> Int -> [String]
centeredTriangle :: Triangle
centeredTriangle c n = [replicate (n-i) ' ' ++ intersperse ' ' (replicate i c) | i <- [0 .. n]]
Output:
Ok, one module loaded.
ghci> centeredTriangle '*' 6
[" "," *"," * *"," * * *"," * * * *"," * * * * *","* * * * * *"]
When I run my main function, I use unlines
to print out the triangle like so:
triangles :: Int -> Int -> Int -> IO()
triangles a b c = do
putStr $ unlines $ centeredTriangle '*' a
putStr $ unlines $ centeredTriangle '*' b
putStr $ unlines $ centeredTriangle '*' c
Output:
ghci> triangles 1 2 3
*
*
* *
*
* *
* * *
I want to print the triangles on the same line, like so:
ghci> triangles 1 2 3
*
* * *
* * * * * *
I realise it may be a bigger task than I first anticipated, but my first thought was to use centeredTriangle
to get the bottom line (last element of the list) of each triangle, and put them toghether in a new string which I then put as the last element in a new list of strings. I thought If I do this for each element (starting from the last) and up to the top I can print the triangles in the same line using my main funtion. How do I achieve this?