Skip to content

Instantly share code, notes, and snippets.

@wereHamster
Created March 4, 2013 10:50
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 wereHamster/5081484 to your computer and use it in GitHub Desktop.
Save wereHamster/5081484 to your computer and use it in GitHub Desktop.
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