Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
reduce :: [Int] -> [Int]
reduce (x:rest@(y:z)) = (x + y) : (reduce rest)
reduce _ = []
pascal :: Int -> [Int]
pascal 1 = [1]
pascal 2 = [1,1]
pascal x = [1] ++ reduce (pascal (x - 1)) ++ [1]
center :: Int -> String -> String
center width string =
let padding = floor (fromIntegral (width - (length string)) / 2)
in (take padding $ repeat ' ') ++ string
main = do
putStrLn ""
putStrLn $ center 40 $ show $ pascal 1
putStrLn $ center 40 $ show $ pascal 2
putStrLn $ center 40 $ show $ pascal 3
putStrLn $ center 40 $ show $ pascal 4
putStrLn $ center 40 $ show $ pascal 5
putStrLn $ center 40 $ show $ pascal 6
putStrLn $ center 40 $ show $ pascal 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment