CS代写 {- butrfeld -}

{- butrfeld -}
module Ex02 where

name, idno, username :: String

Copyright By PowCoder代写 加微信 powcoder

name = “Myself, Me” — replace with your name
idno = “01234567” — replace with your student id
username = “memyselfi” — replace with your TCD username

declaration — do not modify this
, This exercise is all my own work.”
, Signed: ” ++ name
, “++idno++” “++username

— Datatypes and key functions ———————————————–

— do not change anything in this section !

type Id = String

= Val Double
deriving (Eq, Show)

type Dict k d = [(k,d)]

define :: Dict k d -> k -> d -> Dict k d
define d s v = (s,v):d

find :: Dict String d -> String -> Either String d
find [] name = Left (“undefined var “++name)
find ( (s,v) : ds ) name | name == s = Right v
| otherwise = find ds name

type EDict = Dict String Double

v42 = Val 42 ; j42 = Just v42

— do not change anything above !

— Part 1 : Evaluating Expressions — (50 test marks, worth 25 Exercise Marks) –

— Implement the following function so all ‘eval’ tests pass.

— eval should return `Left msg` if:
— (1) a divide by zero operation was going to be performed;
— (2) the expression contains a variable not in the dictionary.
— see test outcomes for the precise format of those messages

eval :: EDict -> Expr -> Either String Double
eval d e = error “eval NYI”

— Part 2 : Expression Laws — (15 test marks, worth 15 Exercise Marks) ——–

There are many, many laws of algebra that apply to our expressions, e.g.,

x * y = y * z Law 1
(x + y) + z = x + (y + z) Law 2
(x / y) / z = x / (y * z) Law 3
(x + y)*(x – y) = x*x – y*y Law 4

We can implement these directly in Haskell using Expr

Function LawN takes an expression:
If it matches the “shape” of the law lefthand-side,
it replaces it with the corresponding righthand “shape”.
If it does not match, it returns Nothing

Implement Laws 1 through 4 above

— x * y = y * z Law 1
law1 :: Expr -> Maybe Expr
law1 e = error “law1 NYI”

— (x + y) + z = x + (y + z) Law 2
law2 :: Expr -> Maybe Expr
law2 e = error “law2 NYI”

— (x / y) / z = x / (y * z) Law 3
law3 :: Expr -> Maybe Expr
law3 e = error “law3 NYI”

— (x + y)*(x – y) = x*x – y*y Law 4
law4 :: Expr -> Maybe Expr
law4 e = error “law4 NYI”

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com