-- Work by: Anas Faisal s0677473 (competition entry included in 11) import LSystem --1. join join :: [Command] -> Command join [] = Sit join xs = foldr1 (:#) xs --2. split split :: Command -> [Command] split Sit = [] split (x:#y) = (split x) ++ (split y) split z = [z] --3. copy copy :: Int -> Command -> Command copy n xs = join (replicate n xs) -- hexagon hexagon :: Distance -> Command hexagon x = copy 6 (Go x :# Turn 60.0) -- pentagon pentagon :: Distance -> Command pentagon x = copy 5 (Go x :# Turn 72.0) --5. polygon polygon :: Distance -> Int -> Command polygon x y = copy y (Go x :# Turn (fromIntegral 360/ fromIntegral y)) --6. spiral spiral :: Distance -> Int -> Distance -> Angle -> Command spiral x y z n | y>1 = (Go x :# Turn n) :# spiral (x-z) (y-1) z n | y>0 = (Go x :# Turn n) | otherwise = Sit --7. optimise optimise :: Command -> Command optimise xs = join (optimise2 ((optimise1 (optimise2 (split xs))))) where { optimise1 []= []; optimise1 (Go x : Turn y : xs) = Go x : optimise1 (Turn y : xs); optimise1 (Turn x : Go y : xs) = Turn x : optimise1 (Go y: xs); optimise1 (Go x: Go y: xs) = optimise1 (Go (x+y) : optimise1 xs); optimise1 (Turn x : Turn y: xs) = optimise1 (Turn (x+y) : optimise1 xs); optimise1 xs = xs; optimise2 [] = []; optimise2 xs = filter (\x -> x/= Turn 0 && x/= Sit && x/= Go 0) xs;} --8. arrowhead arrowhead :: Int -> Command arrowhead x = f x where { f 0 = GrabPen black :# Go 10; f (x + 1) = g x :# p :# f x :# p :# g x; g 0 = GrabPen red :# Go 10; g (x + 1) = f x :# n :# g x :# n :# f x; n = Turn 60; p = Turn (-60); } --9. snowflake snowflake :: Int -> Command snowflake x = f x :# n :# n :# f x :# n :# n :# f x :# n :# n where { f 0 = GrabPen black :# Go 10; f (x + 1) = f x :# p :# f x :# n :# n :# f x :# p :# f x; n = Turn 60; p = Turn (-60); } --10. peanoGosper :: Int -> Command peanoGosper x = f x where { f 0 = GrabPen black :# Go 10; f (x + 1) = f x :# p :# g x :# p :# p :# g x :# n :# f x :# n :# n :# f x :# f x :# n :# g x :# p; g 0 = GrabPen red :# Go 10; g (x + 1) = n :# f x :# p :# g x :# g x :# p :# p :# g x :# p :# f x :# n :# n :# f x :# n :# g x; n = Turn 60; p = Turn (-60); } --11. Competition treee :: Int -> Command treee x = f x where { f 0 = GrabPen blue :# Go 6; f (x + 1) = f x :# Branch ( n :# f x) :# Branch (f x) :# Branch ( p :# f x ) :# f x :# Branch (n:# f x) :# Branch (f x) :# Branch (p:# f x) :# f x :# Branch (n:# f x) :# Branch( f x):# Branch (p:# f x) ; n = Turn 35; p = Turn (-35); }