COMP203 Midterm Exam

Name:
Please submit your answers in a .txt document on the Blackboard site. There is an "assignment" link for this purpose in the Midterm section of the site. You may also submit a copy of this paper with your work handwritten, as a backup.

You are allowed a textbook, calculator and notes on this exam. You are also allowed to use a computer to run Logo, view Blackboard (including past homework assignments), your W: drive, the course web site (http://webhost.bridgew/edu/hburgiel/COMP203), the textbook (http://www.cs.berkeley.edu/~bh/v1-toc2.html) and the user manual (http://www.cs.berkeley.edu/~bh/usermanual).

You are not permitted to use other computer software such as a chat client, email, or Facebook.

If you get stuck on a problem, leave it blank and return to it later. You will receive partial credit for code with minor errors, but please check your work if you have time.


  1. Is the procedure flower below a command or an operation? Justify your answer.
    to flower
     repeat 5 [repeat 36 [forward 10 right 10]~
               right 72]
    end
    

     

     

     

     

  2. Write a procedure that accepts a number as input and then draws a pentagon whose side length equals that number.

  3. Write a predicate longp that takes a list as input and outputs "false if the list contains more than three items and "true otherwise. (Hint: use count.)

     

     

     

     

  4. Write a procedure counter that accepts a number as input and uses while to count down from that number to 0.
    ? counter 5
    5
    4
    3
    2
    1
    0
    ? counter 3
    3
    2
    1
    0
    ?
    
  5. Professor Burgiel wants a program to write more bad poetry, but the one below doesn't work. Find and correct as many errors as you can.
    to story
     local verb
     local noun
     getwords
     if equalp (last "noun) "s [make otherverb "are] [make "otherverb is]
     print (sentence :noun :otherverb word :adjective ", ~
            [but still can] word :verb [!]
    end
    
    to getwords
     local "adjective
     print [Type in a noun.]
     make "noun readword
     print [Type in a verb.]
     make verb "readword
     print [Type in an adjective.]
     make :adjective :readword
    end
    
    If the procedure runs correctly it should behave as shown below.
    ? story
    Type in a noun, singular or plural.
    cat
    Type in a verb.
    hunt
    Type in an adjective.
    sly
    cat is sly, but still can hunt!
    ? story
    Type in a noun, singular or plural.
    trees
    Type in a verb.
    grow
    Type in an adjective.
    green
    trees are green, but still can grow!
    ? 
    

    Bonus: (5 points) Write a recursive procedure that accepts a number n as input and outputs the sum 1 + 2 + 3 + ... + :n.