Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {--
- - Homework 1
- -Authors: Coulby Nguyen, Tanner Rousseau
- -4/25/2018
- --}
- module Hw1 where
- import Prelude hiding (Num)
- --1 A--
- -- We decided that instead of combining 2 Cmds we'd have a Cmd also be a list of Cmd created by the Seq constructor
- -- We also decided to represent Pars as a string list of strings and Vals as a list of ints to hold their respected values
- data Cmd = Pen Mode
- | MoveTo Pos Pos
- | Def String Pars Cmd
- | Call String Vals
- | Seq [Cmd]
- data Mode = Up
- | Down
- data Pos = I Int
- | S String
- type Pars = [String]
- type Vals = [Int]
- --1 B--
- -- here we decided to create a vector with 4 arguments x1 y1 x2 y2
- -- The order of Pen Up
- -- MoveTo(x1,y1)
- -- Pen Down
- -- MoveTo(x2,y2)
- vector = Def "vector" ["x1", "x2", "x3", "x4"] (Seq [Pen Up, MoveTo(S "x1")(S "y1"), Pen Down, MoveTo(S "x2")(S "y2"), Pen Up])
- --1 C--
- --here we started with the base case of 1 or less which should be represented by a single step.
- --originally we wanted to have a base case of 0 but we didn't know how to represent an empty sequence
- --The inductive step then calls the function again with a step value less than the one that was originally called with it
- --And adds that sequence of steps to the previous sequences of steps
- steps :: Int -> Cmd
- 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)]
- 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))]]
- --2 A--
- --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
- data Gates = G Int GateFn Gates
- | NoGates
- data GateFn = And
- | Or
- | Xor
- | Not
- deriving Show
- data Links = AddLink Int Int Int Int Links
- | NoLinks
- data Circuit = Join Gates Links
- --2 B--
- --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
- halfadder = Join(G 1 Xor (G 2 And NoGates))(AddLink 1 1 2 1 (AddLink 1 2 2 2 NoLinks))
- --2 C--
- --This function concatenates the values x and y into a string seperated with a period
- --The next function returns the string from num.num to num.num with num.num being replaced by the pNumNum function
- --The next function returns the string that shows which gate is being looked at and what the type of gate that is
- --The last function calls the pGates and the PLinks functions
- pNumnum :: Int -> Int -> String
- pNumnum x y = show x ++ "." ++ show y
- pLinks :: Links -> String
- pLinks NoLinks = ""
- plinks (AddLink a b c d links) = "from" ++ pNumnum a b ++ "to" ++ pNumnum c d ++ ";\n" ++ pLinks links
- pGates :: Gates -> String
- pGates NoGates = ""
- pGates (G i gtype gates) = show i ++ ":" ++ show gtype ++ ";\n" ++ pGates gates
- pCircuit :: Circuit -> String
- pCircuit (Join gates links) = pGates gates ++ pLinks links
- --3 A--
- data Expr = E Int
- | Plus Expr Expr
- | Times Expr Expr
- | Neg Expr
- data Op = Add | Multiply | Negate
- deriving Show
- data Exp = Num Int
- | Apply Op[Exp]
- deriving Show
- --This is the abstract syntax of (-(3+4)*7)
- -- 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
- result = Apply Multiply [ Apply Negate [Apply Add[Num 4, Num 3]], Num 7]
- --3 B--
- --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.
- --3 C--
- -- for this we found the respective translation found in the expr function as in the Exp function
- translate :: Expr -> Exp
- translate(E x) = (Num x)
- translate(Plus x y) = Apply Add [(translate x), (translate y)]
- translate(Times x y) = Apply Multiply [(translate x), (translate y)]
- translate (Neg x) = Apply Negate [(translate x)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement