Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q4: Swap Implement swap, a procedure which takes a call expression expr and returns the same expression with its first two operands swapped if

 

Q4: Swap Implement swap, a procedure which takes a call expression expr and returns the same expression with its first two operands swapped if the evaluated value of the second operand is greater than the evaluated value of the first operand. Otherwise, it should just return the original expression. For example, (swap '(-1 (+ 3 5) 7)) should return the expression (- (+ 3 5) 1 7) since 1 evaluates to 1, (+ 3 5) evaluates to 8, and 8 > 1. Any operands after the first two should not be evaluated during the execution of the procedure, and they should be left unchanged in the final expression. You may assume that every operand evaluates to a number and that there are always at least two operands in expr. Hint: Quasiquotation might not be the best way to approach this problem (look at the bindings in the let expression to see why this might be the case). What other methods of building lists could we use to solve this problem? Q1: WWSD: Quasiquote Programs as Data Q2: If Program Q3: Exponential Powers Q4: Swap lab12.scm-lab12 - Visual Studio Code File Edit Selection View Go Run Terminal Help EXPLORER lab12.scm x Reminder: Don't forget to evaluate the first two operands when comparing them! (define (cddr s) (cdr (cdr s)) > (define (cadr s) > (car (cdr s)) (define (caddr s) (car (cddr s)) > (define (swap expr) (let ((op (car expr)) ) (first (car (cdr expr))) (second (caddr expr)) (rest (cdr (cddr expr)))) 'YOUR-CODE-HERE ) Q LAB12 > editor > tests 1 2 lab12.scm (define (if-program condition if-true if-false) 'YOUR-CODE-HERE) 3 = .ok_history 4 (define (pow-expr n p) 'YOUR-CODE-HERE) lab12.ok 5 lab12.scm 6 (define (cddr s) (cdr (cdr s))) ok 7 =scheme 8 (define (cadr s) (car (cdr s))) 9 10 (define (caddr s) (car (cddr s))) 11 12 (define (swap expr) 13 (let ((op (car expr)) 14 15 16 17 (first (car (cdr expr))) (second (caddr expr)) (rest (cdr (cddr expr)))) 'YOUR-CODE-HERE)) 18 4 %

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

Computer Architecture Fundamentals And Principles Of Computer Design

Authors: Joseph D. Dumas II

2nd Edition

1032097337, 978-1032097336

More Books

Students also viewed these Algorithms questions