{- Pascal's triangle example, by Janet Hollier CSE 341, Autumn 2008 -} module Pascal (pascal) where {- pascal: creates a list of lists, where each sublist represents a row as defined within Pascal's Triangle. That is, each row begins and ends with 1 and the values in between are the sums of each sequential pair of numbers in the preceding row. Useful for many things, such as taking powers of a binomial. -} pascal ::[[Integer]] pascal = [1] : [1,1] : formrows (tail pascal) {- formrows: helper function for pascal that takes the tail of 'pascal', which is a list of lists beginning with [1,1], and recursively makes a new list, each time from the values of the current list, "x". -} formrows :: [[Integer]] -> [[Integer]] formrows (x:xs) = (nextrow x) : (formrows xs) {- nextrow: helper function that builds each row list by placing a '1' on each end and the sums of the previous row's number pairs in between. -} nextrow :: [Integer] -> [Integer] nextrow x = [1] ++ (addmiddle x) ++ [1] {- addmiddle: adds each sequential pair of numbers in a list together to get the values that will go into the subsequent row. -} addmiddle :: [Integer] -> [Integer] addmiddle (1:[]) = [] addmiddle (x:xs) = (x + (head xs)) : addmiddle xs {- showTriangle: displays Pascal's Triangle (left-justified) one row per line -} showTriangle = do putStrLn "Pascal's Triangle" putStrLn "" let pt = pascal showRows pt {- showRows: shows each row of the triangle -} showRows pt = do let row = head pt let rowStr = strRow row putStrLn rowStr showRows (tail pt) {- strRow: converts an incoming list to a string representation -} strRow :: [Integer] -> String strRow [] = [] strRow (x:xs) = (show x) ++ " " ++ strRow xs