-- Informatics 1 - Functional Programming -- William Bradshaw -- -- To run simply type 'wb' in GHCi import LSystem -- Start Command wb = display (Branch (GrabPen a :#: Go 75 :#: fibcurve6 4 7 a) :#: Turn 180 :#: Branch (GrabPen b2 :#: roots 7) :#: sig) -- Signature sig = GrabPen Inkless :#: Turn 50 :#: Go 400 :#: GrabPen black :#: Turn 330 :#: Go 15 :#: Turn 125 :#: Go 12 :#: Turn 230 :#: Go 12 :#: Turn 125 :#: Go 15 :#: Turn 30 :#: Branch (Go 30) :#: Branch (Turn 180 :#: Go 6):#: Turn 300 :#: Go 10 :#: Turn 290 :#: Go 4 :#: Turn 290 :#: Go 4 :#: Turn 340 :#: negfib 4 -- From Tutorial split :: Command -> [Command] split (p :#: q) = (split p)++(split q) split (Sit) = [] split (x) = [x] join :: [Command] -> Command join [] = Sit join (x:xs) = x :#: join xs copy :: Int -> Command -> Command copy x y = join (replicate x y ) --Pens a, b, c, d, e, f, g, h, i, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, g1, g2, g3, b1, b2, b3, b4, b5 :: Pen a = Colour 0.8 0.6 0.0 b = Colour 0.7 0.625 0.0 c = Colour 0.6 0.65 0.0 d = Colour 0.5 0.675 0.0 e = Colour 0.4 0.7 0.0 f = Colour 0.3 0.725 0.0 g = Colour 0.2 0.75 0.0 h = Colour 0.1 0.775 0.0 i = Colour 0.0 0.8 0.0 f1 = Colour 0.3 0.7 0.3 f2 = Colour 0.4 0.5 0.4 f3 = Colour 0.5 0.3 0.5 f4 = Colour 0.6 0.1 0.6 f5 = Colour 0.7 0.0 0.7 f6 = Colour 0.725 0.625 0.225 f7 = Colour 0.75 0.65 0.25 f8 = Colour 0.775 0.675 0.275 f9 = Colour 0.8 0.7 0.30 f10 = Colour 0.825 0.725 0.325 g1 = Colour 0.1 0.7 0.6 g2 = Colour 0.2 0.5 0.8 g3 = Colour 0.3 0.3 1.0 b1 = Colour 0.8 0.6 0.0 b2 = Colour 0.7 0.5 0.0 b3 = Colour 0.7 0.4 0.0 b4 = Colour 0.7 0.3 0.0 b5 = Colour 0.7 0.2 0.0 z :: Pen -> Pen z y | y == a = b | y == b = c | y == c = d | y == d = e | y == e = f | y == f = g | y == g = h | y == h = i | y == i = f1 | y == f1 = f2 | y == f2 = f3 | y == f3 = f4 | y == f4 = f5 | y == f5 = f5 | y == g1 = g2 | y == g2 = g3 | y == g3 = g3 | y == b1 = b2 | y == b2 = b3 | y == b3 = b4 | y == b4 = b5 | y == b5 = b5 -- Basics fib :: Distance -> Distance fib 0 = 0 fib 1 = 1 fib x = fib (x-1) + fib (x-2) curve :: Distance -> Command curve x = copy 90 (Go (x/pi) :#: Turn 1) anticurve :: Distance -> Command anticurve x = copy 90 (Go (x/pi) :#: Turn 359) -- Original Fibcurves fibcurve :: Distance -> Command fibcurve 0 = Sit fibcurve 1 = curve 1 fibcurve x = curve (fib x) :#: fibcurve (x-1) antifib :: Distance -> Command antifib 0 = Sit antifib 1 = anticurve 1 antifib x = anticurve (fib x) :#: anticurve (x-1) -- Branching Fibcurve fibcurve2 :: Distance -> Command fibcurve2 0 = Sit fibcurve2 1 = curve 1 fibcurve2 x = (Branch (antifib2 (x-1))) :#: curve (fib x) :#: (fibcurve2 (x-1)) antifib2 :: Distance -> Command antifib2 0 = Sit antifib2 1 = anticurve 1 antifib2 x = (Branch (fibcurve2 (x-1))) :#: anticurve (fib x) :#: (antifib2 (x-1)) -- Negative Fibcurve negfib :: Distance -> Command negfib 0 = Sit negfib 1 = curve 1 negfib x = curve (fib x) :#: Turn 180 :#: negfib (x-1) -- Delayed Start Fibcurves fibcurve3 :: Distance -> Command fibcurve3 x = curve x :#: fibcurve2 (x-1) antifib3 :: Distance -> Command antifib3 x = anticurve x :#: antifib2 (x-1) -- Coloured Fibcurves fibcurve4 :: Distance -> Pen -> Command fibcurve4 0 y = Sit fibcurve4 1 y = curve 1 fibcurve4 x y = (Branch ((GrabPen (z y) :#: antifib4 (x-1) (z y)))) :#: GrabPen y :#: curve (fib x) :#: GrabPen (z y) :#: (fibcurve4 (x-1) (z y)) antifib4 :: Distance -> Pen -> Command antifib4 0 y = Sit antifib4 1 y = anticurve 1 antifib4 x y = (Branch ((GrabPen (z y) :#: fibcurve4 (x-1) (z y)))) :#: GrabPen y :#: anticurve (fib x) :#: GrabPen (z y) :#: (antifib4 (x-1) (z y)) -- Flowers fibcurve5 :: Distance -> Distance -> Pen -> Command fibcurve5 q 0 y = Sit fibcurve5 q 1 y = star2 8 (q-2) f2 fibcurve5 q x y = (Branch ((GrabPen (z y) :#: antifib5 q (x-1) (z y)))) :#: GrabPen y :#: curve (fib x) :#: GrabPen (z y) :#: (fibcurve6 q (x-1) (z y)) antifib5 :: Distance -> Distance -> Pen -> Command antifib5 q 0 y = Sit antifib5 q 1 y = anticurve 1 antifib5 q x y = (Branch ((GrabPen (z y) :#: fibcurve6 q (x-1) (z y)))) :#: GrabPen y :#: anticurve (fib x) :#: GrabPen (z y) :#: (antifib6 q (x-1) (z y)) fibcurve6 :: Distance -> Distance -> Pen -> Command fibcurve6 q 0 y = Sit fibcurve6 q 1 y = star2 8 (q-2) g1 fibcurve6 q x y = (Branch ((GrabPen (z y) :#: antifib5 q (x-1) (z y)))) :#: GrabPen y :#: curve (fib x) :#: GrabPen (z y) :#: (fibcurve5 q (x-1) (z y)) antifib6 :: Distance -> Distance -> Pen -> Command antifib6 q 0 y = Sit antifib6 q 1 y = anticurve 1 antifib6 q x y = (Branch ((GrabPen (z y) :#: fibcurve5 q (x-1) (z y)))) :#: GrabPen y :#: anticurve (fib x) :#: GrabPen (z y) :#: (antifib5 q (x-1) (z y)) -- Stars star :: Distance -> Command star x = Branch (fibcurve4 x a) :#: Turn 90 :#: Branch (fibcurve4 x a) :#: Turn 90 :#: Branch (fibcurve4 x a):#: Turn 90 :#: fibcurve4 x a star2 :: Int -> Distance -> Pen -> Command star2 z 0 y = Sit star2 z 1 y = Sit star2 z x y = copy z (Branch (fibcurve6 x x y) :#: Turn (360/(fromIntegral z))) -- Roots roots :: Int -> Command roots 0 = Sit roots x = Branch (p :#: Go 50 :#: roots (x-1)) :#: Branch (q :#: Go 50 :#: roots (x-1)) where p = Turn (15) q = Turn (345)