/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 6 Alyssa P. Hacker doesn't see why... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Alyssa P. Hacker doesn't see why if needs to be provided as a special form. "Why can't I just define it as an ordinary procedure in terms of cond?" she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if: (define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) Eva demonstrates the program for Alyssa: (new-if (=2 3) 0 5) 5 (new-if (= 1 1) 0 5) 0 Delighted, Alyssa uses new-if to rewrite the s (define (sqrt-iter guess \(x\) ) (new-if (good-enough? guess \(x\) ) $$ \begin{array}{l}\text { guess } \\ \text { (sqrt-iter (improve guess } x \text { ) } \\ x) \text { ) }\end{array} $$ What happens when Alyssa attempts to use this to compute square roots? Explain.

Short Answer

Expert verified
Alyssa's `new-if` causes infinite recursion in `sqrt-iter` due to both clauses being evaluated, unlike `if`.

Step by step solution

01

Understanding the problem

Alyssa wants to redefine the `if` construct using `cond` as `new-if`. This seems to work with separate test cases, but you need to see what happens when using `new-if` in a recursive function like `sqrt-iter` to calculate square roots.
02

Exploring the test cases

When calling `(new-if (= 2 3) 0 5)`, the result is `5` because `2` is not equal to `3`, so the else clause is executed. Similarly, `(new-if (= 1 1) 0 5)` returns `0` as the condition is true, and the then clause is executed. These results are expected for a normal if-condition statement.
03

Examining the `sqrt-iter` function

The `sqrt-iter` function is designed to improve the guess for the square root of a number `x`. It uses recursion: if `good-enough?` is true, return `guess`; otherwise, call `sqrt-iter` again with an improved guess. Alyssa replaces the `if` with `new-if`, without realizing that `new-if` behaves differently in a recursive context.
04

Analyzing `new-if` in recursion

Alyssa's `new-if` function is fundamentally a procedure. In Scheme, all the arguments to a procedure are evaluated before the procedure is applied. Thus, both the `then-clause` and `else-clause` will always be evaluated in `new-if`, regardless of the `predicate`. This is different from the built-in `if`, where only one clause is evaluated depending on the condition.
05

Understanding why `new-if` fails in recursion

Because both `then-clause` and `else-clause` are evaluated at each recursive call, the function never terminates. The `else-clause` calls `sqrt-iter` if `good-enough?` is false, which results in an infinite recursion because it doesn't wait for the condition to decide whether or not to evaluate the `else-clause`.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Conditional Expressions
Conditional expressions in programming allow decision-making within your code. They control which block of code should be executed based on certain conditions. In the Scheme programming language, the `if` statement is one of the most common conditional expressions. It allows execution branching based on a boolean condition.
A typical `if` expression in Scheme looks like this:
  • `(if condition then-expression else-expression)`
Here’s how it works:
  • If `condition` evaluates to true, `then-expression` is executed.
  • If `condition` evaluates to false, `else-expression` is executed.
This selective execution helps in making decisions and is fundamental in controlling the flow of your program.
Scheme Programming Language
Scheme is a minimalist, multi-paradigm programming language. It's a functional language derived from Lisp, emphasizing simplicity and flexibility. Understanding Scheme helps students appreciate core programming concepts like recursion and higher-order functions.
  • Simplicity: Scheme has a simple and compact syntax, making code easier to read and write.
  • First-class functions: Functions can be used as arguments to other functions, returned as values from other functions, or assigned to variables.
  • Recursion: Scheme supports recursion elegantly, providing a natural way to repeat computation over iterative approaches.
Understanding Scheme gives valuable insights into functional programming, which is becoming increasingly relevant today. It also teaches you how to think about problems and solutions in a different way compared to more imperative languages.
Procedures
In Scheme, procedures are similar to functions in other programming languages. They are blocks of code you can call by name to perform a specific task. Learning about procedures helps in organizing code to be more reusable and easier to understand.
  • Defining a procedure: You use `(define (procedure-name parameters) body)` to create a function.
  • Calling a procedure: Use the procedure name followed by its arguments in parentheses. E.g., `(procedure-name arg1 arg2)`
Procedures help you reduce code duplication by encapsulating repetitive logic. They make programs modular by breaking down complex tasks into simpler parts. This increases code readability and maintainability, which are important in both small scripts and large applications.
Recursion
Recursion is a key concept in functional programming and is supported naturally in Scheme. A recursive process is where a function calls itself to solve a problem.
In the `sqrt-iter` example, recursion is used to iteratively improve the guess for the square root.
  • Base Case: The function should stop calling itself when it reaches a condition that provides a solution. For `sqrt-iter`, this happens when the guess is "good enough."
  • Recursive Case: If not at the base case, the function calls itself with updated parameters to get closer to the solution.
Recursion is powerful but can be tricky. Each recursive call must bring the problem closer to a solution to avoid infinite loops, as seen with Alyssa's `new-if`. With practice, recursion becomes a natural and effective tool for solving problems involving repeated application of a procedure.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures: (define \((\mathrm{p})(\mathrm{p}))\) (define (test \(\mathrm{x} \mathrm{y}\) ) (if \((=\mathrm{x} 0)\) 0 \(\mathrm{y})\) ) Then he evaluates the expression (test \(0(\mathrm{p}))\) What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normalorder evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)

a. An infinite continued fraction is an expression of the form $$ f=\frac{N_{1}}{D_{1}+\frac{N_{2}}{D_{2}+\frac{N_{3}}{D_{3}+\cdots}}} $$ As an example, one can show that the infinite continued fraction expansion with the \(N_{i}\) and the \(D_{i}\) all equal to 1 produces \(1 / \phi\), where \(\phi\) is the golden ratio (described in section 1.2.2). One way to approximate an infinite continued fraction is to truncate the expansion after a given number of terms. Such a truncation-a so-called \(k\)-term finite continued fraction - has the form $$ \frac{N_{1}}{D_{1}+\frac{N_{2}}{\ddots+\frac{N_{K}}{D_{K}}}} $$ Suppose that \(\mathrm{n}\) and \(\mathrm{d}\) are procedures of one argument (the term index \(i\) ) that return the \(N_{i}\) and \(D_{i}\) of the terms of the continued fraction. Define a procedure cont-frac such that evaluating (cont- frac \(\mathrm{n} \mathrm{d} \mathrm{k}\) ) computes the value of the \(k\)-term finite continued fraction. Check your procedure by approximating \(1 / \phi\) using \(\begin{aligned} \text { (cont-frac (lambda (i) } & 1.0 \text { ) } \\ &\text { (lambda (i) } 1.0) \\ & \text { k) } \end{aligned}\) for successive values of \(\mathrm{k}\). How large must you make \(\mathrm{k}\) in order to get an approximation that is accurate to 4 decimal places? b. If your cont-frac procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process.

A function \(f\) is defined by the rule that \(f(n)=n\) if \(n<3\) and \(f(n)=\) \(f(n-1)+2 f(n-2)+3 f(n-3)\) if \(n \geq 3\). Write a procedure that computes \(f\) by means of a recursive process. Write a procedure that computes \(f\) by means of an iterative process.

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

The sine of an angle (specified in radians) can be computed by making use of the approximation \(\sin x \approx x\) if \(x\) is sufficiently small, and the trigonometric identity \(\sin x=3 \sin \frac{x}{3}-4 \sin ^{3} \frac{x}{3}\) to reduce the size of the argument of \(\sin\). (For purposes of this exercise an angle is considered "sufficiently small" if its magnitude is not greater than \(0.1\) radians.) These ideas are incorporated in the following procedures: (define (cube \(x\) ) \((* x x x)\) ) (define (p \(x\) ) \((-(* 3 x)(* 4(\) cube \(x)))\) ) (define (sine angle) (if (not (> (abs angle) 0.1)) angle (p (sine (/ angle 3.0))))) a. How many times is the procedure \(\mathrm{p}\) applied when (sine 12.15) is evaluated? b. What is the order of growth in space and number of steps (as a function of \(a\) ) used by the process generated by the sine procedure when (sine a) is evaluated?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.