List the different ways that the position argument to ttt can be illegal. (Two ways count as different if they would be checked with different code.)
Define a Scheme procedure named updated-hands that takes two arguments. The first is a list of two hands, each a sentence of cards, with each card being a word in which the first letter is the suit and the rest is the rank. The second is a rank. Updated-hands should return the result—again, a list of two hands—of transferring a card of the given rank from the second hand to the first. You may assume that the second hand contains at least one card of the given rank, and that hands do not contain duplicate cards. Here's an example:
(updated-hands '((ha hk c2) (d5 c6 h10 d10 c8)) 10 )
|((h10 ha hk c2) (d5 c6 d10 c8))
((d10 ha hk c2) (d5 c6 h10 c8))
In the table below, list the arguments to each call to match-using-known-values that results from evaluating the expression
(match '(* a b) '(c a b))You will not necessarily fill in all the blanks. Entries in the table should appear in the sequence that the corresponding calls to match-using-known-values are made (i.e. in the sequence you would see them if you traced match-using-known-values).
Devise a pattern that determines if its sentence argument contains the word "hundred", associating the name "before-hundred" with whatever words occur in the sentence prior to the "hundred" and associating the name "after-hundred" with whatever words occur later.
Fill in the blanks in the following procedure that, given as argument the result of matching a correct answer to part b with the name of an integer between 100 and 1000, returns the integer.
(define (numeric-value match-result) (+ (* 100 (at-most-99-value _____)) (at-most-99-value _____ ) ) )
Assume that a procedure at-most-99-value has been defined that, given a sentence naming a number less than 100, returns its numeric value.
Consider a data base for a programming class, represented as a list, each element of which has the form
(student-name project partner-list)
There is one element in the data base list for each student in the class. The element contains the student's name, the name of the project she worked on, and a list of the other students in the class with whom she worked in partnership on the project. This list does not include the student herself.
For this problem you are to write two versions of a procedure num-submissions that, given a data base and the name of a project, returns the number of submissions of that project. One project per partnership will be submitted, so each person in a partnership of size N will submit 1/N projects. Partnerships in this class may be of any size (unlike in CS 3 this semester).
Write a version of num-submissions that does not use higher-order procedures. (It may use recursion.) You may use helper procedures, as long as they too do not use higher-order procedures.
Write a version of num-submissions that does not use recursion. (It may use higher-order procedures.) You may use helper procedures, as long as their definitions do not use recursion.
Given below is a version of the flatten procedure from one of the lab activities. It doesn't work correctly.
; L is a list. Return the list of the atoms that occur in L, ; in the left-to-right order they appear in L. (define (flatten L) (if (null? L) '( ) (reduce append (map flatten L)) ) )
By filling in the blank below, provide a nonempty argument to flatten that does not cause an error when the resulting expression is evaluated.
(flatten ____ )
List the steps of the evaluation of (flatten '(A B)). (That is, imitate the Replacement Modeler.) If the result is an error, describe the error message and give the expression whose evaluation produced the error.
Which of the following is a reason that flatten doesn't work correctly? (More than one may apply.)
Explain your answer(s).
Making as few changes as possible to flatten, fix it. Here's a copy of the code; indicate clearly what should be inserted, what should be deleted, and what should be changed.
(define (flatten L) (if (null? L) '( ) (reduce append (map flatten L)) ) )
Consider the following higher-order procedure, which returns the result of combining some of the elements in its list argument into lists.
(define (some-paired L pred?) (cond ((null? L) L) ; case 1 ((null? (cdr L)) L) ; case 2 ((pred? (car L) (cadr L)) ; case 3; #t means don't pair (cons (car L) (some-paired (cdr L) pred?)) ) (else ; case 4 (cons (list (car L) (cadr L)) (some-paired (cddr L) pred?) ) ) ) )
One may use some-paired to write a procedure grouped-into-frames that groups balls in a bowling game into frames, leaving strikes ungrouped. Grouped-into-frames, given the list
(9 1 0 10 10 10 6 2 7 3 8 2 10 9 0 2 7)
would thus return the list
((9 1) (0 10) 10 10 (6 2) (7 3) (8 2) 10 (9 0) (2 7))
Fill in the blank below with a procedure that completes the definition of grouped-into-frames in terms of some-paired. Don't add anything else to the grouped-into-frames procedure; you may, however, supply a helper procedure. Assume for this part that the argument to grouped-into-frames is a list representing a game with a miss in the tenth frame.
(define (grouped-into-frames ball-list) (some-paired ball-list ______ ) )
What would be the result of supplying the ball list below to your procedure from part a? (Note the strikes in the tenth frame.) Briefly explain your answer by identifying the cases in some-paired that handle the last three balls.
(9 1 0 10 10 10 6 2 7 3 8 2 10 9 0 10 10 8)
Consider a procedure named 1-extra? that, given two words as inputs, should return true exactly when the first word is the result of inserting a single letter into the second word. Examples:
|(1-extra? 'HEAT 'HAT)||true|
|(1-extra? 'THAT 'HAT)||true|
|(1-extra? 'HATE 'HAT)||true|
|(1-extra? 'ABBC 'ABC)||true|
|(1-extra? 'AT 'HAT)||#f|
|(1-extra? 'HAT 'HAT)||#f|
|(1-extra? 'HATED 'HAT)||#f|
|(1-extra? 'CHEAT 'HAT)||#f|
A buggy version of 1-extra? appears below.
(define (1-extra? big small) (= (num-matches big small) (- (count big) 1)) ) (define (num-matches big small) (cond ((empty? small) 0) ((equal? (first big) (first small)) (+ 1 (num-matches (bf big) (bf small)))) (else (num-matches (bf big) small)) ) )
Provide a call to 1-extra? for which it does not perform as intended, that is, it returns true or crashes when #f is the correct answer, or it returns #f or crashes when true is the correct answer.
Describe, as completely as possible, the set of words wd for which evaluating the expression
(1-extra? wd 'HAT)
produces the correct answer. Briefly explain your answer.
Consider the following procedure, intended to return the result of inserting commas in the correct places (every third position starting from the right) in its nonnegative integer argument.
(define (with-commas n) (if (< n 1000) n (word (with-commas (quotient n 1000)) "," (remainder n 1000)) ) )
For example, (with-commas 12345678) should return the word 12,345,678. The procedure above, however, has a bug.
Describe, as completely as possible, the set of nonnegative integer arguments for which with-commas does not work correctly.
Fix the bug, changing the with-commas procedure as little as possible. You may define a helper procedure.