-- Michael Eldridge -- 0561946 -- Inf1A Functional Programming Assignment 4 import LSystem import List -- I thought I could make use of the unfoldr command for Q2... -- 1. join :: [Command] -> Command join xs = foldr1 (:#) [x | x <- xs] -- join xs = foldr (:#) xs -- 2. split :: Command -> [Command] split x = undefined -- split xs = unfoldr xs (:#) -- 3. copy :: Int -> Command -> Command copy y x = undefined -- copy y xs = replicate y [x | x <- xs] -- 4. hexagon :: Distance -> Command hexagon x = Go x :# Turn 60 :# Go x :# Turn 60 :# Go x :# Turn 60 :# Go x :# Turn 60 :# Go x :# Turn 60 :# Go x :# Turn 60 -- hexagon x = copy 6 (x) -- I assume I should have used the 'copy' function here... -- 5. polygon :: Distance -> Int -> Command polygon x = undefined -- polygon x y = copy y (Go x :# Turn (360/y)) -- 6. spiral :: Distance -> Int -> Distance -> Angle -> Command spiral a b c d = undefined --spiral a b c d = replicate (c/a) (Go (a - c) :# Turn d) -- 7. optimise :: Command -> Command optimise = undefined -- 8. arrowhead :: Int -> Command arrowhead x = f x where f 0 = GrabPen red :# Go 10 f (x+1)= g x :# n :# f x :# n :# g x g 0 = GrabPen blue :# Go 10 g (x+1)= f x :# p :# g x :# p :# f x n = Turn 60 p = Turn (-60) -- 9. branch :: Int -> Command branch x = g x where f 0 = GrabPen blue :# Go 10 f (x+1)= f x :# f x g 0 = GrabPen white :# Go 10 g (x+1)= f x :# Branch ( Branch (g x) :# n :# g x) :# f x :# Branch (n :# f x :# g x) :# p :# g x n = Turn 22.5 p = Turn (-22.5) -- 10. hilbert :: Int -> Command hilbert x = l x where l 0 = GrabPen red :# Go 10 l (x+1) = n :# r x :# f x :# p :# l x :# f x :# l x :# p :# f x :# r x :# n r 0 = GrabPen blue :# Go 10 r (x+1) = p :# l x :# f x :# n :# r x :# f x :# r x :# n :# f x :# l x :# p f 0 = GrabPen green :# Go 10 f (x+1) = f x n = Turn 90 p = Turn (-90) ----------------------------------------------------------------------------------------- -- Inf1A Programming Contest -- Michael Eldridge, 0561946 -- To run in GHCI type in: -- *Main> display 0.4 (rosemary 4) -- Might take some time loading in... rosemary :: Int -> Command rosemary x = g x where f 0 = GrabPen green :# Go 0.5 f (x+1)= g x :# Branch (Branch (f x) :# p :# f x) :# g x :# Branch (p :# g x :# f x) :# n :# f x :# Branch (g x :# n :# g x) :# f x :# Branch (n :# f x :# g x) :# p :# g x :# Branch (f x :# g x) :# f x :# Branch (branch x) g 0 = GrabPen blue :# Go 0.5 g (x+1)= f x :# Branch (Branch (g x) :# n :# g x) :# f x :# Branch (n :# f x :# g x) :# p :# g x :# Branch (f x :# p :# f x) :# g x :# Branch (p :# g x :# f x) :# n :# f x :# Branch (g x :# f x) :# g x :# Branch (branch x) n = Turn 20 p = Turn (-20) -------------------------------------------------------------------------- --rosemary1 :: Int -> Command --rosemary1 x = g x -- where -- f 0 = GrabPen green :# Go 0.5 -- f (x+1)= g x :# Branch (Branch (f x) :# p :# f x) :# g x -- :# Branch (p :# g x :# f x) :# n :# f x -- :# Branch (g x :# f x) -- :# Branch (g x :# n :# g x) :# f x -- :# Branch (n :# f x :# g x) :# p :# g x -- :# Branch (f x :# g x) :# f x -- :# Branch (branch x) -- g 0 = GrabPen blue :# Go 0.5 -- g (x+1)= f x :# Branch (Branch (g x) :# n :# g x) :# f x -- :# Branch (n :# f x :# g x) :# p :# g x -- :# Branch (f x :# g x) -- :# Branch (f x :# p :# f x) :# g x -- :# Branch (p :# g x :# f x) :# n :# f x -- :# Branch (g x :# f x) :# g x -- :# Branch (branch x) -- n = Turn 20 -- p = Turn (-20)