Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Informatics 1 - Functional Programming
  2. -- Tutorial 7
  3. --
  4. -- Week 9 - Due: 19/20 Nov.
  5.  
  6.  
  7. import LSystem
  8. import Test.QuickCheck
  9.  
  10. -- Exercise 1
  11.  
  12. -- 1a. split
  13. split :: Command -> [Command]
  14. split (Go x) = [Go x]
  15. split (Turn x) = [Turn x]
  16. split Sit = []
  17. split (ys :#: xs) = split ys ++ split xs
  18.  
  19.  
  20. -- 1b. join
  21. join :: [Command] -> Command
  22. join [x] = x :#: Sit
  23. join (x:xs) = x :#: (join xs)
  24.  
  25. -- 1c  equivalent
  26. equivalent :: Command -> Command -> Bool
  27. equivalent a b = (split a) == (split b)
  28.  
  29. -- 1d. testing join and split
  30. prop_split_join :: Command -> Bool
  31. prop_split_join c = equivalent c (join (split c))
  32.  
  33.  
  34. prop_split :: Command -> Bool
  35. prop_split c = f (split c)
  36.  
  37. f :: [Command] -> Bool
  38. f [] = True
  39. f (Go distance : xs) = f xs
  40. f (Turn angle : xs) = f xs
  41. f (Sit : xs) = False
  42. f ((x :#: y) : xs) = False
  43.  
  44.  
  45.  
  46. -- Exercise 2
  47. -- 2a. copy
  48. copy :: Int -> Command -> Command
  49. copy 0 c = c
  50. copy n c = c :#: copy (n-1) c
  51.  
  52. -- 2b. pentagon
  53. pentagon :: Distance -> Command
  54. pentagon x = copy 5 (Go x :#: Turn 72)
  55.  
  56. -- 2c. polygon
  57. polygon :: Distance -> Int -> Command
  58. polygon distance sides = copy sides (Go distance :#: Turn (360.0/(fromIntegral sides)))
  59.  
  60. pas :: Distance -> Angle -> Command
  61. pas distance angle = Go distance :#: Turn angle
  62.  
  63. -- Exercise 3
  64. -- spiral
  65. spiral :: Distance -> Int -> Distance -> Angle -> Command
  66. spiral startDistance 0 increment angle = (pas startDistance angle)
  67. spiral startDistance n increment angle = (pas startDistance angle) :#: (spiral (startDistance + increment) (n-1) increment angle)
  68.  
  69.  
  70. -- Exercise 4
  71. -- optimise
  72. optimise :: Command -> Command
  73. optimise = undefined
  74.  
  75.  
  76.  
  77. -- L-Systems
  78.  
  79. -- 5. arrowhead
  80. arrowhead :: Int -> Command
  81. arrowhead = undefined
  82.  
  83. -- 6. snowflake
  84. snowflake :: Int -> Command
  85. snowflake = undefined
  86.  
  87. -- 7. hilbert
  88. hilbert :: Int -> Command
  89. hilbert = undefined
  90.  
  91. main :: IO ()
  92. main = display (Go 30 :#: Turn 120 :#: Go 30 :#: Turn 120 :#: Go 30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement