CSC 550: Introduction to Artificial Intelligence
CSC 650: Advanced Artificial Intelligence
Spring 2003

HW1: Scheme Programming


For this assignment, you are to write numerous Scheme functions, culminating in a program that us able to generate "random" sentences based on grammar rules. Be sure to define each specified function, and provide comments that document the behavior of each function.

  1. Define a function named coin-flip that simulates the flip of a coin. For example, the call (coin-flip) should return either 'heads or 'tails, with equal likelihood.

  2. Define a function named random-element that has one input, a list, and returns a random element of that list. For example, the call (random-element '(a b c)) should evaluate to either 'a, 'b, or 'c, with equal likelihood.

  3. Define a function named arb-dupes that has one input, an atom, and returns a list containing an arbitrary number of copies of that atom. For example, (arb-dupes foo) might evaluate to '(foo), or '(foo foo foo), or even '(). HINT: Think about flipping a coin -- if it comes up heads you add a copy of the atom to a list and flip again; if it comes up tails, you quit with what you have.

  4. Define functions that generate random sentences. Your code should make use of the following grammar rules: sentence <-- noun-phrase + verb-phrase noun-phrase <-- article + noun verb-phrase <-- verb + noun-phrase article <-- the | a noun <-- man | woman | ball | table verb <-- hit | liked | took | saw The first grammar rule is interpreted as saying that "a sentence is a noun-phrase followed by a verb-phrase." The last grammar rule is interpreted as saying that "a verb is one of the following: hit, liked, took, or saw." (| represents a choice). Define a function that corresponds to each of these parts of speech. For example, the call (verb) should evaluate to a random verb, e.g., 'hit. The call (sentence) should evaluate to a random sentence, e.g., '(the man saw a table). You may add more words to the vocabulary of your sentence generator to spice it up.

  5. The random sentence generator you just wrote can be made more interesting with the addition of optional elements and recursion. Consider the following grammar rules: sentence <-- noun-phrase + verb-phrase noun-phrase <-- article + [adjective] + noun + {prep-phrase} verb-phrase <-- verb + noun-phrase prep-phrase <-- preposition + noun-phrase article <-- the | a noun <-- man | woman | ball | table verb <-- hit | liked | took | saw adjective <-- big | little | blue | green | aquatic preposition <-- above | below | by The expression [adjective] specifies an arbitrarily long sequence of adjectives (0 or more occurrences of adjective). The expression {prep-phrase} specifies an optional prepositional phrase (0 or 1 occurrence of prep-phrase).