; File: count4.rkt ; Author: Mihaela Malita ; Iteration with do: ; (do ( ( ) ...) ( ...) ; ...) ; >(start) ; Input a number, n? 10 ; 123456789 ; 123456789 ; 123456789 ; Bye ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (start) (let ((n 0)) (display " Input a number, n? ") (set! n (read)) (count0 n) (newline) (count1 n) (newline) (count2 n) (display "\nBye") )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;iteration with do (define (count0 n) (do ( (i 1 (+ i 1)) ) ((= i n) #t) (display i) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; use a loop structure (define (count1 n) (let myloop ((i 1)) (cond ((= i n)) (else (display i) (set! i (+ 1 i)) (myloop i)) ) )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (count2 n) ;use call-with-current-continuation or call/cc: forces exit from a loop (call/cc (lambda (exit) (do ( (i 1 (+ i 1)) ) (#f) ; loops forever because test is always #f (cond ((= i n) (exit i)) (else (display i))) )) )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (start)