CS 115 – SOS Review Package – Midterm

# Refer to the card structure and data definitions in

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for each of the following applications of the function my-fun? Answer Yes or No. a. (my-fun “hello” ‘hello) b. (my-fun “hello” ‘hello 5) c. (my-fun “” 3.4) d. (my-fun “-123” -123) Is the contract satisfied for each of the following possible produced values of the function my ­fun? Answer Yes or No. e. true f. (cons true (cons 6 empty)) g. empty h. (cons false empty) 4. The function num-vowels consumes a string and produces the number of characters in the string that are values (a, e, i, o, u). If the string does not contain any vowels, then it produces ‘vowel-free. The function header is: (define (num-vowels astring)…) a. Give a contract of num ­vowels b. Give a purpose for num ­vowels c. Give examples for num ­vowels 5. Recall the structure and data definitions of a card. (define-struct card (value suit)) ;;A card is a structure (make-card v s), where v is an integer in the rang from 1 to 10 and s is a symbol from the set ‘hearts, ‘diamonds, ‘spades, and ‘clubs. When comparing two cards to determine if one is larger than the other, we first consider the values of the cards. However, if the values of the cards are the same, then we use the suit of the card to determine which card is larger. The suits are ranked in the following order: spades is greater than hearts, hearts is greater than diamonds, and diamonds is greater than clubs. a. Complete the function higher-suit. Suggestion: Carefully consider the order in which you check the suits of the cards ;;higher-suit: symbol symbol -> boolean ;;Purpose: Consumes s1 and s2 representing different suits of cards. Produces true when s1 is greater than s2 and false otherwise. ;;Examples: (higher-suit ‘spades ‘clubs) -> true ;; (higher-suit ‘diamonds ‘hearts) -> false (define (higher-suit s1 s2) ...) b. Complete the function card>? that consumes two cards card1 and card2 (that do not both have the same value and same suit) and produces true when card1is greater than card2. Use higher-suit from part (a) in your solution. ;;card>?: card card -> boolean ;;Purpose: Produces true when card1 is greater than card2 and false otherwise. ;;Examples: ;;(card>? (make-card 3 ‘spades) (make-card 4 ‘diamonds)) -> false ;;(card>? (make-card 6 ‘hearts) (make-card 6 ‘clubs)) -> true (define (card>? card1 card2) ...) c. Write two tests for that test different situations. Do not repeat the situations from the two provided examples. 6. Complete the function suit-count that consumes a list of card structures and a suit and produces the total value of the cards in the list that match the given suit. Refer to the card structure and data definitions in ques...
View Full Document

## This note was uploaded on 01/19/2014 for the course CS 115 taught by Professor Troyvasiga during the Fall '13 term at University of Waterloo, Waterloo.

Ask a homework question - tutors are online