COMP203: Assignment 4


Please include your full name and the assignment number in your document. Please format your assignment so that it is easy to copy and paste blocks of code into Logo.
  1. Use the Logo command for to write a Logo instruction that lists the multiples of 3 between 0 and 42.

  2. The procedure roll below simulates rolling a six sided die. Use while to write a procedure rollfor6 that uses roll to simulate rolling a die until the result is a 6.
    to roll
     output 1 + (random 6)
    end
    
    Running your procedure should look something like:
    ? rollfor6
    2
    5
    6
    ? rollfor6
    1
    1
    5
    3
    4
    1
    6
    ?
    

  3. Use for to write a procedure named nest that draws a set of nested polygons (e.g. squares or triangles). Use penup and pendown to make usre your polygons appear disconnected.

  4. The Logo procedures for and map are similar but not the same. Below are two simple procedures that take a name as input and output the initials in that name. Which do you think is better? Why?
    to initials.for :name 
     ; code copied from page 78 of Computer Science Logo Style
     local "result
     make "result []
     for [i 1 [count :name]] ~
         [make "result sentence :result first (item :i :name)]
     output :result
    end
    
    to initials.map :name
     output map "first :name
    end
    
  5. Below is a procedure that uses for to accept a list as input and output a new list in which the value of every item in the old list is doubled. Write a program that uses map to do this, or explain what goes wrong when you try to use map.
    to double.list :numlist
     local "newlist
     make "newlist []
     for [i 1 [count :numlist]]~
      [make "newlist (sentence :newlist (2 * (item :i :numlist)))]
     output :newlist
    end
    

Bonus (5 pts): Write an operation initials.while that does the same thing as initials.for and initials.map, but uses while in stead of for or map.