This document summarizes a class about making loops in Scheme. It recaps the Turing machine model and provides an example of a Turing machine that counts stars. It then introduces making loops in Scheme using a for procedure that takes an index, end value, and procedure as arguments. Examples are given to print numbers from 1 to 10 and to generate a multiplication table using nested for loops. The document also discusses tracing for to see how it works and introduces generalized while and accumulating loop procedures. It concludes by announcing assignments and due dates for Problem Set 4 and Exam 1.
1. Class 16:
Making
Loops
cs1120 Fall 2011
David Evans
28 September 2011
2. Plan
Recap: Turing Machine Model
Building a Turing Machine to count stars
(Hint: useful for PS4, Question 3)
Making Loops
(Questions 2 and 3 from Monday, useful for
PS4, Question 12)
PS3 Comments Posted on Web: everyone is
expected to understand these before Exam 1
2
3. Recap: Turing Machine
Model Input, Output, and Scratch Memory
Infinite tape, divided into discrete squares
Each square can contain a single symbol from
a finite alphabet
Model Processing
Finite State Machine
Keep track of a finite state (in your head)
Follow transition rules:
next state, write symbol, move
= f(current state, input symbol)
3
8. Turing Machine Design
Painful and tedious: each step
does very little
But, important to do a few times
to help convince yourself a TM
really can do any computation
Thinking about what the machine
needs to keep track of: states
(finite) and tape (infinite)
Every computer scientist should do it at least once!
You will have to do it at least twice:
Question 3 on PS4, Problem on Exam 1
8
10. Loops in Other Languages
Fortran (1956) Ada (1983)
DO I = 1,9 for I in 1..9 loop
PRINT I PutLine (I);
END DO
Java (1995)
Algol (1960)
for (int i = 1; i < 10; i++) {
for i := 1 until 9 do
System.out.println(i);
print (i);
}
10
11. Making Loops in Scheme
(define (for index end proc)
(if (>= index end)
(void) ; this evaluates to no value
(begin
(proc index)
(for (+ index 1) end proc))))
11
12. Using for
(for 1 10 (lambda (i) (display i)))
Use for to print out a multiplication table:
1x1=1
1x2=2
…
1x9=9
2 x 1 =2
2 x 2 =4
…
2 x 9 =18
…
9 x 9 = 81
12
13. (define (times-table max)
(for 1 max
(lambda (i)
(for 1 max
(lambda (j)
(printf "~a x ~a = ~a~n" i j (* i j)))))))
13
14. (define (for index end proc)
> (require racket/trace)
(if (>= index end)
> (trace for)
(void) ; this evaluates to no value
> (times-table 3)
(begin
>(for 1 3 #<procedure:...class15.rkt:22:4>)
(proc index)
> (for 1 3 #<procedure:...class15.rkt:24:8>)
(for (+ index 1) end proc))))
1x1=1
> (for 2 3 #<procedure:...class15.rkt:24:8>)
(define (times-table max)
1x2=2
(for 1 max
> (for 3 3 #<procedure:...class15.rkt:24:8>)
(lambda (i)
< #<void>
(for 1 max
>(for 2 3 #<procedure:...class15.rkt:22:4>)
(lambda (j)
> (for 1 3 #<procedure:...class15.rkt:24:8>)
(printf "~a x ~a = ~a~n" i j (* i j)))))))
2x1=2
> (for 2 3 #<procedure:...class15.rkt:24:8>)
2x2=4
> (for 3 3 #<procedure:...class15.rkt:24:8>)
< #<void>
>(for 3 3 #<procedure:...class15.rkt:22:4>)
<#<void>
14
16. Generalized Loops
(define (while index test update proc)
(if (test index)
(begin
(proc index)
(while (update index) test update proc))
index))
16
17. Accumulating Results
(define (loop index result test update proc)
(if (test index)
(loop (update index)
(proc index result)
test update proc)
result))
17
18. Charge
PS4 due Monday 3 October
Exam 1: out October 7, due October 12
Covers:
Problem Sets 1-4 including PS Comments
Course Book Chapters 1-6
Classes 1-18
Reading:
no reading assignment until after Exam 1, but I will
start covering things in Chapter 7-8 soon and
encourage you to read The Information, Chapters 5-7
18