Advertisement
Guest User

my comments for homework 1

a guest
Apr 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {--
  2.    - Homework 1
  3.       -Authors: Coulby Nguyen, Tanner Rousseau
  4.       -4/25/2018
  5. --}
  6.  
  7. module Hw1 where
  8.  
  9. import Prelude hiding (Num)
  10.  
  11.  
  12. --1 A--
  13. -- We decided that instead of combining 2 Cmds we'd have a Cmd also be a list of Cmd created by the Seq constructor
  14. -- We also decided to represent Pars as a string list of strings and Vals as a list of ints to hold their respected values
  15. data Cmd = Pen Mode
  16.          | MoveTo Pos Pos
  17.          | Def String Pars Cmd
  18.          | Call String Vals
  19.          | Seq [Cmd]
  20.  
  21. data Mode = Up
  22.          | Down
  23.  
  24. data Pos = I Int
  25.          | S String
  26.  
  27. type Pars = [String]
  28.  
  29. type Vals = [Int]
  30.  
  31.  
  32. --1 B--
  33. -- here we decided to create a vector with 4 arguments x1 y1 x2 y2
  34. -- The order of Pen Up
  35. --              MoveTo(x1,y1)
  36. --              Pen Down
  37. --              MoveTo(x2,y2)
  38.  
  39. vector = Def "vector" ["x1", "x2", "x3", "x4"] (Seq [Pen Up, MoveTo(S "x1")(S "y1"), Pen Down, MoveTo(S "x2")(S "y2"), Pen Up])
  40.  
  41.  
  42. --1 C--
  43. --here we started with the base case of 1 or less which should be represented by a single step.
  44. --originally we wanted to have a base case of 0 but we didn't know how to represent an empty sequence
  45. --The inductive step then calls the function again with a step value less than the one that was originally called with it
  46. --And adds that sequence of steps to the previous sequences of steps
  47.  
  48. steps :: Int -> Cmd
  49. steps a | a <= 1 = Seq [Pen Up, MoveTo(I 0)(I 0), Pen Down, MoveTo(I 0)(I 1), Pen Up, MoveTo(I 0)(I 1), Pen Down, MoveTo(I 1)(I 1)]
  50.  
  51. steps a      = Seq[steps(a-1), Seq[Pen Up, MoveTo(I (a-1))(I (a-1)), Pen Down, MoveTo(I (a-1))(I (a)), Pen Up, MoveTo(I (a-1))(I (a)), Pen Down, MoveTo(I(a))(I(a))]]
  52.  
  53.  
  54.  
  55.  
  56. --2 A--
  57. --This is the gates data type that has 2 constructors, either pass in an int and the type of gate or no NoGates which is the emptycase
  58.  
  59. data Gates = G Int GateFn Gates
  60.              | NoGates
  61.  
  62. data GateFn = And
  63.               | Or
  64.               | Xor
  65.               | Not
  66.           deriving Show
  67.  
  68. data Links = AddLink Int Int Int Int Links
  69.              | NoLinks
  70.  
  71. data Circuit = Join Gates Links
  72.  
  73.  
  74.  
  75.  
  76. --2 B--
  77. --This is the half adder circut that we created that joins 2 circuits  the Xor and the And operators with the following values 1 1 2 1 to 1 2 2 2
  78. halfadder = Join(G 1 Xor (G 2 And NoGates))(AddLink 1 1 2 1 (AddLink 1 2 2 2 NoLinks))
  79.  
  80. --2 C--
  81. --This function concatenates the values x and y into a string seperated with a period
  82. --The next function returns the string from num.num to num.num with num.num being replaced by the pNumNum function
  83. --The next function returns the string that shows which gate is being looked at and what the type of gate that is
  84. --The last function calls the pGates and the PLinks functions
  85. pNumnum :: Int -> Int -> String
  86. pNumnum x y = show x ++ "." ++ show y
  87.  
  88.  
  89. pLinks :: Links -> String
  90. pLinks NoLinks = ""
  91. plinks (AddLink a b c d links) = "from" ++ pNumnum a b ++ "to" ++ pNumnum c d ++ ";\n" ++ pLinks links
  92.  
  93. pGates :: Gates -> String
  94. pGates NoGates = ""
  95. pGates (G i gtype gates) = show i ++ ":" ++ show gtype ++ ";\n" ++ pGates gates
  96.  
  97. pCircuit :: Circuit -> String
  98. pCircuit (Join gates links) = pGates gates ++ pLinks links
  99.  
  100.  
  101.  
  102. --3  A--
  103.  
  104. data Expr = E Int
  105.       | Plus Expr Expr
  106.       | Times Expr Expr
  107.       | Neg Expr
  108.  
  109. data Op = Add | Multiply | Negate
  110.     deriving Show
  111.  
  112. data Exp = Num Int
  113.     | Apply Op[Exp]
  114.     deriving Show
  115.  
  116. --This is the abstract syntax of (-(3+4)*7)
  117. -- so we worked inside out with adding the num 4 and num 3 then applying negate to that value, and lastly multiplying that value by 7
  118.  
  119. result = Apply Multiply [ Apply Negate [Apply Add[Num 4, Num 3]], Num 7]
  120.  
  121. --3  B--
  122. --One signnificant advantage of the alternative syntax is that it offers the ability to operate on multiple values at the same time. The first syntax is simple and easily readable due to the binary design of prefix notation, however lacks the complexity that the second offers.
  123.  
  124.  
  125. --3  C--
  126. -- for this we found the respective translation found in the expr function as in the Exp function
  127. translate :: Expr -> Exp
  128. translate(E x) = (Num x)
  129. translate(Plus x y) = Apply Add [(translate x), (translate y)]
  130. translate(Times x y) = Apply Multiply [(translate x), (translate y)]
  131. translate (Neg x) = Apply Negate [(translate x)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement