Skip to main content

Lab: Booleans values, predicates, and conditionals

Held
Wednesday, 13 February 2019
Writeup due
Friday, 15 February 2019
Summary
In this lab, you will have the opportunity to explore the use of predicates as well as Scheme’s three primary conditional control operations, if, when, and cond.

Selected useful procedures and notation

Some important regular expressions

^ - the start of a string (when not in square brackets)

$ - the end of a string

. - any character

[A-Z] - any capital letter

Boolean values

#t - the value used to represent “true”

#f - the value used to represent “false”

List operations

(tally lst pred?) - Count the number of values in lst for which pred? holds.

(filter pred? lst) - Extract all values in lst for which pred? holds.

(if condition consequent alternative) Standard keyword. - Evaluate condition. If its value is truish (that is, anything but false), evaluate consequent and return its value. If the value of the condition is false (#f), evaluate and return alternative.

(cond [guard-1 consequents-1] [guard-2 consequents-2] ... [guard-n consequents-n] [else alternative]) Standard keyword. - Evaluate each guard in turn until one is truish. It then evaluates the corresponding sequence of consequent expressions and returns the value of the last consequent. If none of the guards is truish, evaluates the alternative and returns its value.

(when guard exp1 exp2 ... expn) Optional Scheme Keyword. - Evaluate guard. If it holds, evaluate each expression in turn. Otherwise, do nothing.

(and exp1 exp2 ... expn) Standard keyword. - Evaluate each expression in turn. If any of those values is false, return false. Otherwise, return the value of the last expression.

(or exp1 exp2 ... expn) Standard keyword. - Evaluate each expression in turn. If any of those values is truish, return the first truish value. Otherwise, return false.

String operations

(substring str start finish) - Extract a portion of str, starting at position start and finishing immediately before position finish.

Preparation

a. Do the traditional lab preparation. That is,

  • Open a terminal window and type /home/rebelsky/bin/csc151/update.
  • Start DrRacket.
  • Require the loudhum package with (require loudhum).
  • Require the 2htdp/image package with (require 2htdp/image).

b. If you have not done so already, make a list of the important procedures and keywords described in the reading on Boolean values and predicates and the reading on conditionals and what they do. You might copy and paste into your definitions pane and “comment out” your notes using #| and |#.

c. Add the following to your definitions pane.

; Some words, as strings
(define story-beginning
  (list "Once" "upon" "a" "time" "in" "a" "land" "not" "so" "far" "away"))

; The words in Jane Eyre (plus whatever Project Gutenberg adds)
(define eyre-words (file->words "/home/rebelsky/Desktop/pg1260.txt"))

Exercises

Exercise 1: Building shorter lists

The eyre-words list is a bit long and the story-beginning list is a bit simple and predictable. Let’s consider ways to extract a different set of words to work with.

a. What does the following instruction do?

> (define p-words (filter (section regexp-match? #px"^.p" <>) eyre-words))

b. Check your answer experimentally

c. What does the following instruction do?

> (define some-words (take p-words 50))

d. Check your answer experimentally.

e. Add the two definitions to your definitions pane.

Exercise 2: Word prefixes

a. Write a procedure, (starts-with-capital? str), that takes a string as input and returns true if it starts with a capital letter and false otherwise.

b. Using starts-with-capital?, determine which elements of some-words start with a capital letter.

c. How would you check your answer to 2b to make sure that it’s correct?

Exercise 3: String suffixes

a. Write a procedure, (ing? str), that takes a string as input and returns true if the string ends with "ing" and false otherwise.

b. Using ing?, determine which elements of some-words end with ing.

c. If you wrote ing? using regular expressions, write a new version that does not use regular expressions (e.g., by taking a substring of the last three letters and comparing them to "ing").
If you wrote ing? without regular expressions, rewrite ing? using regular expressions (e.g., by writing a regular expression that matches "ing" at the end of a string).

Exercise 4: Word lengths

a. Write a procedure, (long? str), that takes a string as input and returns true (#t) if the string is at least eight characters and false (#f) otherwise.

b. Using long?, determine how many words in some-words have at least eight characters.

c. Using long?, determine how many words in some-words have fewer than eight characters. (Hint: You will find negate useful.)

d. Using long? and ing?, determine how many words in some-words have at least eight characters and end in "ing". (Hint: You will find conjoin or disjoin useful.)

e. Using long? and ing?, determine how many words in some-words have fewer than eight characters and end in "ing".

Exercise 5: From strings to shapes

In this exercise, you will define and test some procedures that takes one argument, a string, and returns (i) a small blue circle if the word ends in "ing", (ii) a small red triangle if the word ends in "ly", or (iii) a small black square in every other case.

a. Use if (and not when or cond) to define a procedure, (string->shape-a str), that behaves as described.

b. Use cond (and not if or when) to define a procedure, (string->shape-b str), that behaves as described.

Exercise 6: From strings to shapes, continued

a. What do you expect the result of the following expression to be? (You need not describe it precisely.)

> (map string->shape-b some-words)
?

b. Check your answer experimentally.

c. What do you expect the result of the following expression to be? (You need not describe it precisely.)

> (reduce beside (map string->shape-b some-words))

d. Check your answer experimentally.

Exercise 7: Categorizing values

In the reading, we started to write a procedure that categorized values. Let’s extend that example.

Write a procedure, (type-of val), that returns

  • the symbol 'boolean, if val is a boolean;
  • the symbol 'character, if val is a character;
  • the symbol 'number, if val is a number;
  • the symbol 'procedure, if val is a procedure;
  • the symbol 'string, if val is a string;
  • the symbol 'symbol, if val is a symbol; or
  • the symbol 'miscellaneous, if val is anything else.

Exercise 8: The Sphinx’s riddle

As you may know, one of the famous riddles of the Sphinx goes something like the following:

What is it that walks upon four legs, then two legs, then three legs?

The answer is, of course, humans.

Write a Scheme procedure, (legs age), that, given someone’s age, tells how many legs they walk upon. (You get to choose reasonable ages for these three phases of life.)

Exercise 9: The Sphinx’s Riddle, revisited

You recently wrote a procedure, (legs age), that, given someone’s age, tells how many legs they walk upon. It is likely that you wrote that procedure using if or cond. However, it is also possible to write legs using only and, or, and not. Try doing so.

For those with extra time

If you find that you finish this lab early, you might try one or more of the following exercises.

Extra 1: From strings to shapes, revisited

Define and test a procedure (string->shape str) that takes one argument, a string, and returns (i) a small blue circle if the word ends in "ing", (ii) a small red triangle if the word ends in "ly", or (iii) a small black square in every other case. The “size” of the shape (however you decide to define it) should be 5 + twice the number of characters in the string. For example, the “size” of “example” would be 19 (5 + 2*7).

Extra 2: Validating dates

Write a procedure, (valid-date? month day), that returns #t if the numbers month and day describe a valid month and day and false otherwise. For example,

> (valid-date? 1 30)
#t
> (valid-date? 2 30)
#f
> (valid-date? 9 30)
#t
> (valid-date? 9 31)
#f
> (valid-date? 9 -1)
#f
> (valid-date? 9 250)
#f

a. Write this procedure using if as the primary control structure.

b. Write this procedure using cond as the primary control structure.

c. Write this procedure without if and cond (that is, using and, or, and, possibly, not).