Class 16: Making Loops

Preview:

DESCRIPTION

Designing Turing MachinesMaking Loops

Citation preview

Class 16: Making Loops

cs1120 Fall 2011David Evans28 September 2011

2

Plan

Recap: Turing Machine ModelBuilding 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

3

Recap: Turing Machine

Model Input, Output, and Scratch MemoryInfinite tape, divided into discrete squaresEach square can contain a single symbol from a finite alphabet

Model ProcessingFinite State Machine

Keep track of a finite state (in your head)Follow transition rules:

next state, write symbol, move = f(current state, input symbol)

4

Turing Machine Example

1

#/#,-

HALT

0/1,R1/0,R

read/write, move

State, Input, Next, Write, Move1, 1, 1, 0, >1, 0, 1, 1, >1, #, H, #, >

TM Simulator: http://ironphoenix.org/tril/tm/

Input: 01101#

5

Turing Machine Example

Start

#/#,-

HALT

0/0,R1/1,L

State, Input, Next, Write, Move1, 1, 1, 1, <1, 0, 1, 0, >1, #, H, #, >

Input: 01101#

6

Count the StarsGoal (example): ## #1111#

7

http://www.youtube.com/watch?v=cYw2ewoO6c4

8

Turing Machine DesignPainful and tedious: each step

does very littleBut, 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

9

Making Loops

10

Loops in Other Languages

Fortran (1956)DO I = 1,9 PRINT IEND DO

Java (1995)for (int i = 1; i < 10; i++) { System.out.println(i);}

Algol (1960)for i := 1 until 9 do print (i);

Ada (1983)for I in 1..9 loop PutLine (I);

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))))

12

Using for(for 1 10 (lambda (i) (display i)))

Use for to print out a multiplication table:1 x 1 = 11 x 2 = 2

…1 x 9 = 9

2 x 1 =22 x 2 =4

…2 x 9 =18

… 9 x 9 = 81

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)))))))

14

> (require racket/trace)> (trace for)> (times-table 3)>(for 1 3 #<procedure:...class15.rkt:22:4>)> (for 1 3 #<procedure:...class15.rkt:24:8>)1 x 1 = 1> (for 2 3 #<procedure:...class15.rkt:24:8>)1 x 2 = 2> (for 3 3 #<procedure:...class15.rkt:24:8>)< #<void>>(for 2 3 #<procedure:...class15.rkt:22:4>)> (for 1 3 #<procedure:...class15.rkt:24:8>)2 x 1 = 2> (for 2 3 #<procedure:...class15.rkt:24:8>)2 x 2 = 4> (for 3 3 #<procedure:...class15.rkt:24:8>)< #<void>>(for 3 3 #<procedure:...class15.rkt:22:4>)<#<void>

(define (for index end proc) (if (>= index end) (void) ; this evaluates to no value (begin (proc index) (for (+ index 1) end proc))))

(define (times-table max) (for 1 max (lambda (i) (for 1 max (lambda (j) (printf "~a x ~a = ~a~n" i j (* i j)))))))

15

Generalized Loops(while p (lambda (p) (not (null? p))) cdr (lambda (p) (display (car p)))))

16

Generalized Loops

(define (while index test update proc) (if (test index) (begin (proc index) (while (update index) test update proc)) index))

17

Accumulating Results

(define (loop index result test update proc) (if (test index) (loop (update index) (proc index result) test update proc) result))

18

Charge

PS4 due Monday 3 OctoberExam 1: out October 7, due October 12

Covers: Problem Sets 1-4 including PS CommentsCourse Book Chapters 1-6Classes 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

Recommended