Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in (define acc (make-account 100 'secret-password)) The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint: ((acc' 'secret-password 'withdraw) 40) 60 ((acc' 'some-other-password 'deposit) 50) "Incorrect password"

Short Answer

Expert verified
Create an account object using `make-account`, verify password for every operation, and handle operations if the verification is successful.

Step by step solution

01

Define the Procedure

To create a password-protected account, start by defining the procedure `make-account`. This procedure should have an additional parameter to accept the password, say `password`. So, it will be defined as `(define (make-account balance password))`.
02

Define the Account Functionality

Inside the `make-account` procedure, define an internal function that checks the provided password. If the password matches, it performs the requested operation; otherwise, it returns an error message. The internal function should take a password attempt, an operation ('withdraw or 'deposit), and an amount.
03

Implement Password Verification

In your internal function, first check if the provided password attempt equals the original password. Use an `if` statement: `(if (equal? provided-password password) ...)`. If the passwords do not match, return "Incorrect password".
04

Handle Withdraw and Deposit

When the password is correct, use a `cond` expression to handle the operations. For 'withdraw, check if the balance is sufficient before deducting the amount. For 'deposit, simply add the amount to the balance.
05

Return the Account Object

The entire account functionality (password verification and operations handling) is encapsulated in the internal function. The `make-account` procedure should return this function to provide an account object that can be interacted with the methods provided.

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 Vaia!

Key Concepts

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

Scheme programming
Scheme is a simple and elegant programming language that's popular in academic settings. It's a dialect of Lisp and emphasizes minimalism, primarily employing functions and recursion to solve problems.
Scheme is highly valued in education because it encourages clear thinking about code structure and function. Here are some core features you might find useful:
  • **Simple Syntax:** Scheme's syntax is minimal, which makes it easier for learners to understand.
  • **Powerful Abstractions:** It allows you to express your ideas in powerful constructs, such as higher-order functions.
  • **First-Class Procedures:** Functions in Scheme can be treated just like any other data, allowing them to be passed as arguments or returned from other functions.
Overall, Scheme's main benefit lies in its ability to help you focus on how a program should work logically, rather than getting caught up in complex syntax or unnecessary details.
When developing programs like the password-protected account exercise, these features aid in creating precise and efficiently operating code.
higher-order functions
In Scheme, higher-order functions are a standard feature. These are functions that either take other functions as arguments or return them as results. Such flexibility is key in functional programming, and it allows you to build more abstract and reusable code.
In the case of the password-protected account, the `make-account` procedure acts as a higher-order function by returning another function. This returned function embodies the entire account object, encapsulating behavior and state management.
  • **Function as Data:** Treating functions as data allows for more expressive programs.
  • **Abstraction and Encapsulation:** You can hide details and complexity, exposing only necessary interfaces.
  • **Code Reuse:** Encourages writing generic, reusable software components.
Understanding higher-order functions is crucial for mastering Scheme programming. They are an essential tool for building complex systems in a modular and maintainable way.
procedure definition
A procedure definition in Scheme specifies the behavior of a block of code that can be executed by calling it with given arguments. It's akin to defining a 'task' or 'action' that you can perform multiple times with different data.
In the given exercise, the `make-account` procedure outlines the steps necessary to create an account with password protection. This involves defining internal logic to handle operations securely and efficiently.
  • **Defining Procedures:** You use the `define` keyword to create procedures.
  • **Modular Code:** Procedures allow you to modularize your program, cleaning up complex code into manageable pieces.
  • **Parameters:** Within a procedure, parameters serve as placeholders for the actual data passed during the function call.
By mastering procedure definitions, you get to organize your code into logical sections. This can make it easier to read, reduce errors, and promote reuse. In functional programming languages like Scheme, efficient and organized procedure definition is a key aspect of skillful programming.

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

Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren't automatically serialized. He suggests that make-account-and-serializer should have exported the serializer (for use by such procedures as serialized-exchange) in addition to (rather than instead of) using it to serialize accounts and deposits as makeaccount did. He proposes to redefine accounts as follows: (define (make-account-and-serializer balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (let ((balance-serializer (make-serializer))) (define (dispatch m) (cond ((eq? m'withdraw) (balance-serializer withdraw)) (Ceq? m 'deposit) (balance-serializer deposit)) ((eq? m 'balance) balance) ((eq? m 'serializer) balance-serializer) (else (error "Unknown request -- MAKE-ACCot dispatch)) Then deposits are handled as with the original make-account: (define (deposit account amount) ((account' 'deposit) amount)) Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called.

The procedures to be run during each time segment of the agenda are kept in a queue. Thus, the procedures for each segment are called in the order in which they were added to the agenda (first in, first out). Explain why this order must be used. In particular, trace the behavior of an and-gate whose inputs change from 0,1 to 1,0 in the same segment and say how the behavior would differ if we stored a segment's procedures in an ordinary list, adding and removing procedures only at the front (last in, first out).

In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, \(f\), that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to \(\mathrm{mf}\) is the special symbol how-many-calls?, then \(\mathrm{mf}\) returns the value of the counter. If the input is the special symbol reset-count, then \(m f\) resets the counter to zero. For any other input, \(m f\) returns the result of calling \(f\) on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure: (define s (make-monitored sqrt)) \((\mathrm{s} 100)\) 10 \(\left(\mathrm{~s}^{\prime}\right.\) hou-many-calls?) 1

Using primitive multiplier, adder, and constant constraints, define a procedure averager that takes three connectors \(\mathrm{a}, \mathrm{b}\), and \(\mathrm{c}\) as inputs and establishes the constraint that the value of \(c\) is the average of the values of a and \(b\).

A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2,3 , or 5 . One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2,3 , and 5 . But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, let us call the required stream of numbers \(S\) and notice the following facts about it. \- S begins with \(1 .\) \- The elements of (scale-stream S 2) are also elements of S. \- The same is true for (scale-stream \(\mathrm{S} 3\) ) and (scale-stream \(5 \mathrm{~S}\) ). \- These are all the elements of \(\mathrm{S}\). Now all we have to do is combine elements from these sources. For this we define a procedure merge that combines two ordered streams into one ordered result stream, eliminating repetitions: (define (merge s1 s2) (cond ((stream-null? s1) s2) ((stream-nul1? s2) s1) (else (let ( s1car (stream-car s1)) (s2car (stream-car s2))) (cond ((< s1car s2car) (cons-stream s1car (merge (stream-cdr s1) s2))) ((> s1car s2car) (cons-stream s2car (merge s1 (stream-cdr s2)))) (else (cons-stream s1car (merge (stream-cdr s1) Then the required stream may be constructed with merge, as follows: (define \(\mathrm{S}\) (cons-stream 1 (merge \(\langle ? ?\rangle\langle ? ?\rangle)\) ) Fill in the missing expressions in the places marked \(\langle ? ?\rangle\) above.

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.

Sign-up for free