SlideShare a Scribd company logo
Class 16:
                    Making
                    Loops



cs1120 Fall 2011
David Evans
28 September 2011
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
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
Turing Machine Example
read/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
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
Count the Stars
Goal (example): #       #    #1111#




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




                                             7
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
Making
Loops




         9
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
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
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
(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
(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
Generalized Loops
(while p
       (lambda (p) (not (null? p)))
       cdr
       (lambda (p) (display (car p)))))




                                          15
Generalized Loops
(define (while index test update proc)
 (if (test index)
     (begin
       (proc index)
       (while (update index) test update proc))
      index))



                                                  16
Accumulating Results

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


                                               17
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

More Related Content

PPT
Iteration
PDF
Appsec obfuscator reloaded
PDF
Nesting of for loops using C++
PDF
How Triton can help to reverse virtual machine based software protections
PDF
Basic c++ 11/14 for python programmers
PDF
TMPA-2017: Predicate Abstraction Based Configurable Method for Data Race Dete...
PDF
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...
Iteration
Appsec obfuscator reloaded
Nesting of for loops using C++
How Triton can help to reverse virtual machine based software protections
Basic c++ 11/14 for python programmers
TMPA-2017: Predicate Abstraction Based Configurable Method for Data Race Dete...
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...

What's hot (20)

PPT
Verilog Lecture3 hust 2014
PDF
Dynamic Binary Analysis and Obfuscated Codes
PDF
2018 cosup-delete unused python code safely - english
PDF
R/C++ talk at earl 2014
PPTX
06.Loops
PDF
MP in Clojure
PDF
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
PPTX
SHA1 collision analysis and resolving a problem of recursive hashing with xra...
PPTX
Cs1123 6 loops
PPTX
STACK || FUNCTION WRITING BASED ON STACK || DATA STRUCTURE || LINKED LIST || ...
PPT
C Sharp Jn (3)
PDF
PPTX
Loops in Python
PDF
Free Monads Getting Started
PDF
A blast from the past
PPTX
Scope and closures
PPTX
QUEUE || FUNCTION WRITING BASED ON QUEUE || LINKED LIST || DATA STRUCTURE || ...
PPTX
What is to loop in c++
PPT
Verilog Lecture2 thhts
PDF
MapReduce for Parallel Trace Validation of LTL Properties
Verilog Lecture3 hust 2014
Dynamic Binary Analysis and Obfuscated Codes
2018 cosup-delete unused python code safely - english
R/C++ talk at earl 2014
06.Loops
MP in Clojure
Runtime Monitoring of Stream Logic Formulae (Talk @ FPS 2015)
SHA1 collision analysis and resolving a problem of recursive hashing with xra...
Cs1123 6 loops
STACK || FUNCTION WRITING BASED ON STACK || DATA STRUCTURE || LINKED LIST || ...
C Sharp Jn (3)
Loops in Python
Free Monads Getting Started
A blast from the past
Scope and closures
QUEUE || FUNCTION WRITING BASED ON QUEUE || LINKED LIST || DATA STRUCTURE || ...
What is to loop in c++
Verilog Lecture2 thhts
MapReduce for Parallel Trace Validation of LTL Properties
Ad

Similar to Class 16: Making Loops (20)

PPTX
Class 37: Computability in Theory and Practice
PPTX
Class 14: Modeling Computers
PDF
Nov 09 MS32
PDF
June 09 MS1
PDF
Nov 07 MS3
PDF
Nov 02 P2
PPTX
Class 36: Halting Problem
PDF
Automata And Compiler Design
PDF
June 10 P31
PDF
Components - Crossing the Boundaries while Analyzing Heterogeneous Component-...
PPTX
Class 28: Entropy
PDF
07 A1 Ec01 C Programming And Data Structures
PPT
C Language Unit-1
PDF
June 10 P32
PDF
86254 162058-ee2255-digital-logic-circuits
PPTX
Pseudocode flowcharts
PDF
June 10 MS33
PDF
June 03 MS1
PPT
Unit1 jwfiles
PDF
June 10 P33
Class 37: Computability in Theory and Practice
Class 14: Modeling Computers
Nov 09 MS32
June 09 MS1
Nov 07 MS3
Nov 02 P2
Class 36: Halting Problem
Automata And Compiler Design
June 10 P31
Components - Crossing the Boundaries while Analyzing Heterogeneous Component-...
Class 28: Entropy
07 A1 Ec01 C Programming And Data Structures
C Language Unit-1
June 10 P32
86254 162058-ee2255-digital-logic-circuits
Pseudocode flowcharts
June 10 MS33
June 03 MS1
Unit1 jwfiles
June 10 P33
Ad

More from David Evans (20)

PPTX
Cryptocurrency Jeopardy!
PPTX
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
PPTX
Hidden Services, Zero Knowledge
PPTX
Anonymity in Bitcoin
PPTX
Midterm Confirmations
PPTX
Scripting Transactions
PPTX
How to Live in Paradise
PPTX
Bitcoin Script
PPTX
Mining Economics
PPTX
Mining
PPTX
The Blockchain
PPTX
Becoming More Paranoid
PPTX
Asymmetric Key Signatures
PPTX
Introduction to Cryptography
PPTX
Class 1: What is Money?
PPTX
Multi-Party Computation for the Masses
PPTX
Proof of Reserve
PPTX
Silk Road
PPTX
Blooming Sidechains!
PPTX
Useful Proofs of Work, Permacoin
Cryptocurrency Jeopardy!
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Hidden Services, Zero Knowledge
Anonymity in Bitcoin
Midterm Confirmations
Scripting Transactions
How to Live in Paradise
Bitcoin Script
Mining Economics
Mining
The Blockchain
Becoming More Paranoid
Asymmetric Key Signatures
Introduction to Cryptography
Class 1: What is Money?
Multi-Party Computation for the Masses
Proof of Reserve
Silk Road
Blooming Sidechains!
Useful Proofs of Work, Permacoin

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
KodekX | Application Modernization Development
Cloud computing and distributed systems.
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Building Integrated photovoltaic BIPV_UPV.pdf

Class 16: Making Loops

  • 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
  • 4. Turing Machine Example read/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. 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. Count the Stars Goal (example): # # #1111# 6
  • 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
  • 15. Generalized Loops (while p (lambda (p) (not (null? p))) cdr (lambda (p) (display (car p))))) 15
  • 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