Haskell Quiz/PP Pascal/Solution Ltriant

From HaskellWiki
< Haskell Quiz‎ | PP Pascal
Revision as of 04:08, 1 November 2006 by Ltriant (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


Works...kind of; adds a space or two (or three) to the end of each line.

module Main where

import System.Environment ( getArgs )

fac n = product [1..n]
nck n k = (fac n) `div` ((fac $ n - k) * (fac k))
space_len n = mylength $ show $ (n-1) `nck` ((n-1) `div` 2)
mylength = toInteger . length

indent n t =
    (concat $ replicate (fromInteger $ (n-p) * space_len n)  " "):t
    where p = mylength t

space_out n t =
    map (\x -> show x ++ (concat $ replicate (num_spaces x) " ")) t
    where num_spaces x = fromInteger $ (2 * space_len n) - (mylength $ show x)

pp_pascal n =
    map (concat . indent n . space_out n) $ f 0 n
    where f acc n | acc == n  = []
                  | otherwise = (map (nck acc) [0..acc]):(f (acc+1) n)

main =
    do args <- getArgs
       case args of
         [v] -> mapM_ putStrLn $ pp_pascal (read v :: Integer)
         _   -> error "No argument specified."