-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
27 lines (22 loc) · 788 Bytes
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module Main where
main :: IO ()
main = do
putStr "Enter size of triangle (must be greater than or equal to 0): "
input <- getLine
let n = (read input :: Int)
printTriangle n
printTriangle :: Int -> IO ()
printTriangle n = putStrLn $ unlines (triangle n)
triangle :: Int -> [String]
triangle n
| n < 0 = error "can't have negative n"
| n == 0 = smallest
| otherwise = let t = triangle (n-1)
padding = replicate (halfWidthOfTriangle t) ' '
upper = map (\line -> padding ++ line ++ padding) t
lower = zipWith (++) t t
in upper ++ lower
halfWidthOfTriangle :: [String] -> Int
halfWidthOfTriangle = (`div` 2) . length . head
smallest :: [String]
smallest = lines "/\\"