Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

GOAL: Extend the WAE language to include multiply and divide operators (*, /) and a conditional expression: (if <0 expr val1 val2) which has the

GOAL: Extend the WAE language to include multiply and divide operators (*, /) and a conditional expression: (if<0 expr val1 val2) which has the value if is less than zero and val2 otherwise. so, {with {a 5} {if<0 a 77 99}} would have 99. implement divide operator as an integer using quotient so that (> WAE ;; to convert s-expressions into WAEs (define (parse sexp) (cond [(number? sexp) (num sexp)] [(symbol? sexp) (id sexp)] [(list? sexp) (case (first sexp) [(+) (add (parse (second sexp)) (parse (third sexp)))] [(-) (sub (parse (second sexp)) (parse (third sexp)))] [(with) (with (first (second sexp)) (parse (second (second sexp))) (parse (third sexp)))] )] )) ;; subst : WAE symbol WAE --> WAE ;; substitutes second argument with third argument in first argument, ;; as per the rules of substitution; the resulting expression contains ;; no free instances of the second argument ;; (version p.26 of Krishnamurthi) (define(subst expr sub-id val) (type-case WAE expr [num (n) expr] [add (l r) (add (subst l sub-id val) (subst r sub-id val))] [sub (l r) (sub (subst l sub-id val) (subst r sub-id val))] [with (bound-id named-expr bound-body) (if (symbol=? bound-id sub-id) (with bound-id (subst named-expr sub-id val) bound-body) (with bound-id (subst named-expr sub-id val) (subst bound-body sub-id val)))] [id (v) (if (symbol=? v sub-id) val expr)] )) ;; calc : WAE --> number ;; evaluates WAE expressions by reducing them to numbers (define (calc expr) (type-case WAE expr [num (n) n] [add (l r) (+ (calc l) (calc r))] [sub (l r) (- (calc l) (calc r))] [with (bound-id named-expr bound-body) (calc (subst bound-body bound-id (num (calc named-expr))))] [id (v) (error 'calc "free identifier ~a" v)] )) (define e1 (with 'x (num 12) (sub (id 'x) (with 'x (num 2) (add (id 'x) (id 'x))))) ) (define e2 (with 'x (num 12) (sub (add (num 5) (id 'x)) (with 'y (num 2) (add (id 'x) (id 'y))))) ) (test (calc (parse '5)) 5) (test (calc (parse '{+ 5 5})) 10) (test (calc (parse '{with {x {+ 5 5}} {+ x x}})) 20) (test (calc (parse '{with {x 5} {+ x x}})) 10) (test (calc (parse '{with {x {+ 5 5}} {with{y{- x 3}}{+ y y}}})) 14) (test (calc (parse '{with {x 5} {with{y{- x 3}}{+ y y}}})) 4) (test (calc (parse '{with {x 5} {+ x{with{x 3}10}}})) 15) (test (calc (parse '{with {x 5} {+ x{with{x 3}x}}})) 8) (test (calc (parse '{with {x 5} {+ x{with{y 3}x}}})) 10) (test (calc (parse '{with {x 5} {with{y x}y}})) 5) (test (calc (parse '{with {x 5} {with{x x}x}})) 5)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

Explain the functions of financial management.

Answered: 1 week ago

Question

HOW MANY TOTAL WORLD WAR?

Answered: 1 week ago

Question

Discuss the scope of financial management.

Answered: 1 week ago

Question

Discuss the goals of financial management.

Answered: 1 week ago