import Pictures -- -- Code from the January 31 class. -- -- fact n = n factorial -- NOTE: a Program error occurs when fact is given a negative argument fact :: Integer -> Integer fact n | n==0 = 1 | n>0 = n* fact (n-1) -- power2 n = 2^n power2 :: Int -> Int power2 n | n==0 = 1 -- base case | n>0 = 2 * power2(n-1) -- inductive/recursion step -- Fibonacci numbers fib :: Integer -> Integer fib n | n==0 = 0 | n==1 = 1 | n>1 = fib(n-2) + fib(n-1) | otherwise = error "fib given a negative argument" -- powerOf m n -- returns True iff m = n^k for some nonnegative integer k powerOf :: Integer -> Integer -> Bool powerOf m n | m==1 = True -- case 1 | n==1 = False -- case 2 | mod m n /= 0 = False -- case 3 | otherwise = powerOf (div m n) n -- case 4 collatz :: Integer -> Integer collatz n | n == 1 = 1 | odd n = collatz(3*n+1) | otherwise = collatz(n `div` 2) traceCollatz :: Integer -> [Integer] traceCollatz n | n == 1 = [1] | odd n = n:traceCollatz(3*n+1) | otherwise = n:traceCollatz(n `div` 2) ------------------------------------------------------------------------ -- Picture recursions ------------------------------------------------------------------------ blank, blob, horiz, vert, black, exx, what :: Picture blank = [".....", ".....", ".....", ".....", "....."] blob = ["..#..", ".###.", ".###.", ".###.", "..#.."] horiz = ["#####", ".....", "#####", ".....", "#####"] vert = ["#.#.#", "#.#.#", "#.#.#", "#.#.#", "#.#.#"] exx = ["#...#", ".#.#.", "..#..", ".#.#.", "#...#"] what = [".###.", "#...#", "...#.", "..#..", "..#.."] black = invertColour blank pp = printPicture column :: Int -> Picture -> Picture -- column n pic -- returns the picture obtained by combining n copies of pic in a column -- An error occurs if n is less than 1 column n pic | n<1 = error "Column: n < 1" | n==1 = pic | otherwise = above pic (column (n-1) pic) row :: Int -> Picture -> Picture -- row n pic -- returns the picture obtained by combining n copies of pic in a row -- An error occurs if n is less than 1 row n pic | n<1 = error "fuss" | n==1 = pic | otherwise = sideBySide pic (row (n-1) pic) mixedRow, mixedColumn :: Int -> Picture -> Picture -- mixedRow n pic -- returns the picture obtained by combining n copies of pic; -- however, each occurrence of pic has its color inverted from -- the previous occurrence. -- -- For exampe: (mixedRow 4 black) returns a row of boxes of -- color black, blank, black, blank mixedRow n pic | n<1 = error "fuss" | n==1 = pic | otherwise = sideBySide pic (mixedRow (n-1) (invertColour pic)) mixedColumn n pic | n<1 = error "fuss" | n==1 = pic | otherwise = above pic (mixedColumn (n-1) (invertColour pic)) grid :: Int -> Int -> Picture -> Picture -- grid m n pic -- returns the picture obtained by creating an m x n grid -- (i.e., m rows, n columns) of copies of pic grid m n pic = column m (row n pic) mixedGrid :: Int -> Int -> Picture -> Picture -- mixedGrid m n pic -- returns the picture obtained by creating an m x n grid -- (i.e., m rows, n columns) of copies of pic in a checkerboard -- pattern (i.e., alternating colors) mixedGrid m n pic = mixedColumn m (mixedRow n pic) -- I STOPPED HERE!!! above3 :: Picture -> Picture -> Picture -> Picture -- above3 pic1 pic2 pic3 -- returns a new picture consisting of pic1 above pic2 above pic3 above3 pic1 pic2 pic3 = above pic1 (above pic2 pic3) side3 :: Picture -> Picture -> Picture -> Picture -- side3 pic1 pic2 pic3 -- returns a new picture consisting of pic1 beside pic2 beside pic3 side3 pic1 pic2 pic3 = sideBySide pic1 (sideBySide pic2 pic3) altBox :: Int -> Picture -> Picture -- altBox m pic -- returns an m by m grid of pic that alternative in color -- on successive rings altBox n pic = error "fuss"