CS61A Lab 12: Programming in Logo

Week 12, 2011

What Will Logo Print?

Question 0

Try and predict the result of each of the following pieces of Logo. Try each in the Logo interpreter to check your answer. Do all of the lines in order. Make sure you understand why Logo behaves the way it does in each example!

print sum 2 3 4 5

print (sum 2 3 4 5)

pr first first bf [yellow submarine]

show map "first [paperback writer]

show map [word first ? last ?] [lucy in the sky with diamonds]

repeat 5 [print [this boy]]

;Note the ~ symbols!
print ifelse 4=1+2 ~
      ["flying] ~
      [[all you need is love]]

to pr2nd :thing
  print first bf :thing
end

pr2nd [the 1 after 909]
print first pr2nd [hey jude]

to greet :person
  say [how are you,]
end

to say :saying
  print sentence :saying :person
end

greet "ringo

to who :sent
  foreach [pete roger john keith] "describe
end

to describe :person
  print se :person :sent
end

who [sells out]

make "bass "paul

print :bass

make :bass "bass

print :paul

Let's Write Logo Procedures!

Question 1

Write the procedure countdown in Logo which takes a number and prints on separate lines a countdown followed by the line "blastoff!" as shown below:

? countdown 5
5
4
3
2
1
blastoff!

Question 2

Write the procedure downup which behaves in the following way:

? downup "pacman
pacman
pacma
pacm
pac
pa
p
pa
pac
pacm
pacma
pacman
? downup [foo bar baz garply]
[foo bar baz garply]
[foo bar baz]
[foo bar]
[foo]
[foo bar]
[foo bar baz]
[foo bar baz garply]

Hint: There's a procedure in Logo called butlast (or bl for short) which takes either a word and returns the word without the last letter or a sentence and returns a sentence with all but the last item. Also, emptyp works for both sentences and words!

Question 3

Write the procedure double_evens which takes a sentence of numbers and outputs the same sentence with the all of the even values doubled. Here's an example of how it should work:

? show double_evens [4 0 5 2 1]
[8 0 5 4 1]

Question 4

Write the procedure repeated which takes the name of a procedure :fn, a number :n, and an value :arg and outputs the result of applying :fn to :arg :n times. An example of how it should work is shown below:

? print repeated "butlast 3 "downstream
downstr
? print repeated "butfirst 4 "downstream
stream

Hint: you'll probably want to use the procedure run