Question
Please answer these questions using plait in drRacket (The chegg staff answering this must provide test cases and show screenshot outputs to prove the code
Please answer these questions using plait in drRacket (The chegg staff answering this must provide test cases and show screenshot outputs to prove the code works otherwise there is nothing to learn from this due to it being incorrect). Thank you.
Part 1 Maximum
Start with the interpreter with functions Links to an external site., and add a max operator that takes two numbers and returns the larger of them.
Since you must change the Exp datatype, and since different people may change it in different ways, you must update the parse function, which accepts an S-expression and produces an Exp value.
Some examples:
(test (interp (parse `{max 1 2})
(list))
2)
(test (interp (parse `{max {+ 4 5} {+ 2 3}})
(list))
9)
Part 2 Functions that Accept Multiple Arguments
Extend the interpreter to support multiple or zero arguments to a function, and multiple or zero arguments in a function call.
For example,
{define {area w h} {* w h}}
defines a function that takes two arguments, while
{define {five} 5}
defines a function that takes zero arguments. Similarly,
{area 3 4}
calls the function area with two arguments, while
{five}
calls the function five with zero arguments.
At run-time, a new error is now possible: function application with the wrong number of arguments. Your interp function should detect the mismatch and report an error that includes the words wrong arity.
To support functions with multiple arguments, youll have to change fd and appE and all tests that use them. When you update the parse function, note that s-exp-match? supports ... in a pattern to indicate zero or more repetitions of the preceding pattern. Beware of putting the multi-argument application pattern too early in parse, since that pattern is likely to match other forms. In addition, youll need to update the parse-fundef function that takes one quoted define form and produces a Func-Defn value.
Just to clarify: Supporting multiple-argument functions does not mean changing operations like + or *. Although + and * are functions in Plait, theyre treated as non-function operator forms in Curly.
Some examples:
(test (interp (parse `{f 1 2})
(list (parse-fundef `{define {f x y} {+ x y}})))
3)
(test (interp (parse `{+ {f} {f}})
(list (parse-fundef `{define {f} 5})))
10)
(test/exn (interp (parse `{f 1})
(list (parse-fundef `{define {f x y} {+ x y}})))
"wrong arity")
Remember that Plait provides map, which takes a function and a list, and applies the function to each element in the list, returning a list of results. For example, if sexps is a list of S-expressions to parse, (map parse sexps) produces a list of ExprCs by parsing each S-expression.
But also remember that map doesnt work for everything. Sometimes, when you have a list to process (or maybe two lists in parallel), then you need to write a new function using the template for lists.
Part 3 BONUS(5PTS) Function Argument Checking
A function is ill-defined if two of its argument names are the same. To prevent this problem, update your parse-fundef function can detect this problem and report a bad syntax error.
For example, (parse-fundef `{define {f x x} x}) must report a bad syntax error, while (parse-fundef `{define {f x y} x}) should produce a Func-Defn value.
The code from the "interpreter with functions" link to answer the 3 parts:
#lang plait (define-type Exp (numE [n : Number]) (idE [s : Symbol]) (plusE [l : Exp] [r : Exp]) (multE [l : Exp] [r : Exp]) (appE [s : Symbol] [arg : Exp])) (define-type Func-Defn (fd [name : Symbol] [arg : Symbol] [body : Exp])) (module+ test (print-only-errors #t)) ;; An EXP is either ;; - `NUMBER ;; - `SYMBOL ;; - `{+ EXP EXP} ;; - `{* EXP EXP} ;; - `{SYMBOL EXP) ;; A FUNC-DEFN is ;; - `{define {SYMBOL SYMBOL} EXP} ;; parse ---------------------------------------- (define (parse [s : S-Exp]) : Exp (cond [(s-exp-match? `NUMBER s) (numE (s-exp->number s))] [(s-exp-match? `SYMBOL s) (idE (s-exp->symbol s))] [(s-exp-match? `{+ ANY ANY} s) (plusE (parse (second (s-exp->list s))) (parse (third (s-exp->list s))))] [(s-exp-match? `{* ANY ANY} s) (multE (parse (second (s-exp->list s))) (parse (third (s-exp->list s))))] [(s-exp-match? `{SYMBOL ANY} s) (appE (s-exp->symbol (first (s-exp->list s))) (parse (second (s-exp->list s))))] [else (error 'parse "invalid input")])) (define (parse-fundef [s : S-Exp]) : Func-Defn (cond [(s-exp-match? `{define {SYMBOL SYMBOL} ANY} s) (fd (s-exp->symbol (first (s-exp->list (second (s-exp->list s))))) (s-exp->symbol (second (s-exp->list (second (s-exp->list s))))) (parse (third (s-exp->list s))))] [else (error 'parse-fundef "invalid input")])) (module+ test (test (parse `2) (numE 2)) (test (parse `x) (idE 'x)) (test (parse `{+ 2 1}) (plusE (numE 2) (numE 1))) (test (parse `{* 3 4}) (multE (numE 3) (numE 4))) (test (parse `{+ {* 3 4} 8}) (plusE (multE (numE 3) (numE 4)) (numE 8))) (test (parse `{double 9}) (appE 'double (numE 9))) (test/exn (parse `{{+ 1 2}}) "invalid input") (test (parse-fundef `{define {double x} {+ x x}}) (fd 'double 'x (plusE (idE 'x) (idE 'x)))) (test/exn (parse-fundef `{def {f x} x}) "invalid input") (define double-def (parse-fundef `{define {double x} {+ x x}})) (define quadruple-def (parse-fundef `{define {quadruple x} {double {double x}}}))) ;; interp ---------------------------------------- (define (interp [a : Exp] [defs : (Listof Func-Defn)]) : Number (type-case Exp a [(numE n) n] [(idE s) (error 'interp "free variable")] [(plusE l r) (+ (interp l defs) (interp r defs))] [(multE l r) (* (interp l defs) (interp r defs))] [(appE s arg) (local [(define fd (get-fundef s defs))] (interp (subst (numE (interp arg defs)) (fd-arg fd) (fd-body fd)) defs))])) (module+ test (test (interp (parse `2) empty) 2) (test/exn (interp (parse `x) empty) "free variable") (test (interp (parse `{+ 2 1}) empty) 3) (test (interp (parse `{* 2 1}) empty) 2) (test (interp (parse `{+ {* 2 3} {+ 5 8}}) empty) 19) (test (interp (parse `{double 8}) (list double-def)) 16) (test (interp (parse `{quadruple 8}) (list double-def quadruple-def)) 32)) ;; get-fundef ---------------------------------------- (define (get-fundef [s : Symbol] [defs : (Listof Func-Defn)]) : Func-Defn (type-case (Listof Func-Defn) defs [empty (error 'get-fundef "undefined function")] [(cons def rst-defs) (if (eq? s (fd-name def)) def (get-fundef s rst-defs))])) (module+ test (test (get-fundef 'double (list double-def)) double-def) (test (get-fundef 'double (list double-def quadruple-def)) double-def) (test (get-fundef 'double (list quadruple-def double-def)) double-def) (test (get-fundef 'quadruple (list quadruple-def double-def)) quadruple-def) (test/exn (get-fundef 'double empty) "undefined function")) ;; subst ---------------------------------------- (define (subst [what : Exp] [for : Symbol] [in : Exp]) (type-case Exp in [(numE n) in] [(idE s) (if (eq? for s) what in)] [(plusE l r) (plusE (subst what for l) (subst what for r))] [(multE l r) (multE (subst what for l) (subst what for r))] [(appE s arg) (appE s (subst what for arg))])) (module+ test (test (subst (parse `8) 'x (parse `9)) (numE 9)) (test (subst (parse `8) 'x (parse `x)) (numE 8)) (test (subst (parse `8) 'x (parse `y)) (idE 'y)) (test (subst (parse `8) 'x (parse `{+ x y})) (parse `{+ 8 y})) (test (subst (parse `8) 'x (parse `{* y x})) (parse `{* y 8})) (test (subst (parse `8) 'x (parse `{double x})) (parse `{double 8})))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started