Skip to content

Instantly share code, notes, and snippets.

@axman6
Forked from calware/haskellstriangle.hs
Last active October 2, 2019 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axman6/029360216584cd47f36597df04c10c7f to your computer and use it in GitHub Desktop.
Save axman6/029360216584cd47f36597df04c10c7f to your computer and use it in GitHub Desktop.
{-- 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 --}
-- printRow :: (Integral a, Show a) => a -> [a] -> IO ()
printRow :: Int -> [Int] -> IO ()
printRow pad nums = do
let printPad c = putStr [' ' | _ <- [1..c]]
printPad pad
putStrLn [x | ent <- nums, x <- (show ent ++ " " )]
-- genRow :: (Integral a) => [a] -> [a]
genRow :: [Int] -> [Int]
genRow prev = [1] ++ newRow ++ [1]
where newRow = zipWith (+) prev (tail prev)
-- couldn't define a type signature here like i wanted to
-- build :: (Integral a, Show a) => [a] -> a -> IO ()
build :: [Int] -> Int -> IO ()
build row size
| padding < 0 = putChar '\n'
| otherwise = do
printRow padding row
build (genRow row) size
where padding = size - (length row)
main = do
putChar '\n'
build [1] 5
putStrLn ":^)\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment