7.9

Homework 3

home work!

Programming Language BSL

Due Date: Friday February 12, 9pm

Purpose To write functions; design structured data; design World Programs

Expectations

This will be an individual (non-pair) assignment. For this and all future assignments you must upload one .rkt file in the language specified at the top of the assignment to the Handin Server. Failure to do so invalidates your submission and will therefore, unfortunately, result in a score of 0.

Unless otherwise stated, all data and functions you design must be designed according to the data and function design recipes (DR), resp., following all four steps in each case.

-----------------------------------------------------------------------------

The language english is a very small subset of English (hence the lower-case initial). To define this language, we are given the following data designs, some of which are partial. Note that this language does not know upper-case letters (another reason for the lower-case initial in its name).

; An Article is one of "a", "an", "the".
; an article in the english language
(define art-def "the")
(define (article-templ a)
  (... (cond [(string=? a "a")   ...]
             [(string=? a "an")  ...]
             [(string=? a "the") ...])))
 
; A Noun is one of "rooster", "avenue", "vegetable", "orchestra"
; a noun in the english language
 
; A Verb is one of "crows", "winds, "grows", "excites"
(define verb-cry "crows")

Exercise 1 Complete the data designs of Article, Noun, Verb, as necessary. If there is nothing to do, do nothing. If there is something to do: clearly state what you are adding. Your editor’s copy-and-paste function may be your friend.

Exercise 2 Design vocabulary recognizer predicates article?, noun?, verb? for the above three types of data. These functions should take anything as input (you can denote it as Any in your signature) and return true if and only if the argument is an Article, a Noun, or a Verb, resp.

Note that Strings can only be compared using string=?, and that conversely string=? can only be used to compare strings.

We further know: a sentence is a subject phrase followed by a verb. A subject phrase is an article followed by a noun. All english sentences consist of three words.

Exercise 3 Design data to represent a subject phrase and data to represent a sentence. Use the Article, Noun, and Verb data designs to represent articles, nouns, and verbs.

Exercise 4 Design a function render-sentence that takes a Sentence and checks whether the sentence begins with an indefinite article ("a", "an"). If yes, it returns the String "indefinite sentence". If no, it returns the sentence, with spaces between words, rendered as an image, font-size and color of your choice. As always, define the image parameters (size, color) as constants, as well as the "indefinite sentence" message.

Whoops: this function returns either an image or a string—what do we do about the signature? Revisit the signature of the vocabulary recognizers defined earlier; maybe this gives you an idea?

Hint: to prevent this function from becoming too unwieldy, design a helper function that converts a Sentence into a String, irrespective of whether there is an indefinite article at the beginning.

Exercise 5
  • Design data for a vowel. Use common sense here, but if you insist that the letter y is a vowel (as some people do), go for it.

  • Design a recognizer predicate vowel? that takes an Any as input and returns whether the given data is a lower-case vowel. Start from the template of your vowel data, and then consider whether you can simplify the code. Remember what we learned about Boolean simplification! (Do not turn in the intermediate code, before simplification.)

An english sentence is well-formed if

A subject phrase is well-formed if its article and its noun belong to the vocabulary of the english language, AND

In the rest of this homework we will design functions well-formed-sentence? and well-formed-subject-phrase?, resp., that recognize whether a given sentence and subject phrase, resp., are well-formed. This is harder than it looks! We need a number of helper functions.

Exercise 6 Design a function sp-chars that returns the number of characters in a subject phrase.

Exercise 7 Design a function that is given a given natural number, interpreted as the total number of non-space characters in a sentence (this function will be used inside well-formed-sentence?). Your function returns whether that number is a prime. Be smart about this one: a general natural-number primality test is not necessary. Instead: you know the vocabulary of the english language. How many non-space characters can a sentence have at most? what does this tell you about the primes you need to consider? No fancy math needed!

You must explain your reasoning (in a comment).

Also, carefully think about the signature of your primality test function: if you use the above hint to simplify this test, you must express in your signature that this test is only valid for a certain sub-range of natural numbers. Maybe you even need a small data design.

Exercise 8 Design the function well-formed-subject-phrase?, by following the above definition of well-formed subject phrases.

In your check-expects, use the data examples defined earlier. If you need to add data examples suitable as test cases, add it, either here or earlier with the data design.

Exercise 9 Finally, design function well-formed-sentence?. According to its definition, you may need to call well-formed-subject-phrase?do not reinvent the wheel.

Give at least one example of a sentence whose subject phrase is well formed, whose verb belongs to the vocabulary of the english language, but whose total length (non-space characters) is not a prime.