Class 16: Making Loops

650 views

Published on

Designing Turing Machines
Making Loops

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
650
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class 16: Making Loops

  1. 1. Class 16: Making Loopscs1120 Fall 2011David Evans28 September 2011
  2. 2. PlanRecap: 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 2
  3. 3. Recap: Turing MachineModel Input, Output, and Scratch Memory Infinite tape, divided into discrete squares Each square can contain a single symbol from a finite alphabetModel 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
  4. 4. Turing Machine Exampleread/write, move 1/0,R Input: 01101# 0/1,R State, Input, Next, Write, Move 1 1, 1, 1, 0, > 1, 0, 1, 1, > 1, #, H, #, > #/#,- HALT TM Simulator: http://ironphoenix.org/tril/tm/ 4
  5. 5. Turing Machine Example Input: 01101#1/1,L 0/0,R State, Input, Next, Write, Move 1, 1, 1, 1, < Start 1, 0, 1, 0, > 1, #, H, #, > #/#,- HALT 5
  6. 6. Count the StarsGoal (example): # # #1111# 6
  7. 7. http://www.youtube.com/watch?v=cYw2ewoO6c4 7
  8. 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 computationThinking 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
  9. 9. MakingLoops 9
  10. 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. 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. 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. 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. 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
  15. 15. Generalized Loops(while p (lambda (p) (not (null? p))) cdr (lambda (p) (display (car p))))) 15
  16. 16. Generalized Loops(define (while index test update proc) (if (test index) (begin (proc index) (while (update index) test update proc)) index)) 16
  17. 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. 18. ChargePS4 due Monday 3 OctoberExam 1: out October 7, due October 12 Covers: Problem Sets 1-4 including PS Comments Course Book Chapters 1-6 Classes 1-18Reading: 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

×