lab9 - #| = LAB 9 = | | Author(s): | Lab Section: | |# ;...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#| ====== LAB 9 ====== | | Author(s): | Lab Section: | |# ;;;; Utility Functions ; ;; Reloads the current file. (define (reload) (load "lab9.scm") ; Change file name if copied to a new file. ) ;; Test Code ;; ;; Usage: (do-test '(sqrt 9)) ;; Output: (sqrt 9) => 3 ;; ;; Please note the ' before the procedure call. ;; This is required for proper usage. ; (define (do-test test-case) (display test-case) (display " => ") (display (eval test-case user-initial-environment)) (newline)) ;; Square (define (square x) (* x x)) ( (define (count-leaves x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (+ (count-leaves (car x)) (count-leaves (cdr x)))))) ;;;; ;;;; Step 1 - A Quick Warm Up ;;;; (define step1_list1 '(1 3 (5 7) 9)) ;; Answer: (display (car (cdr (car (cdr (cdr step1_list1))))))(newline) ( (define step1_list2 '((7))) ;; Answer: (display (car (car step1_list2)))(newline) ( (define step1_list3 '(1 (2 (3 (4 (5 (6 7))))))) ;; Answer: (display (cdr step1_list3))(newline) (display (car (cdr step1_list3)))(newline) (display (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr step1_list3)))))))))))))(newline) s ;;;;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
;;;; Step 2 - More on Abstractions ;;;; ; (define (expmod base exp m) (define square (lambda (x) (* x x))) (cond ((= exp 0) 1) ((even? exp) (remainder (square (expmod base (/ exp 2) m)) m)) (else (remainder (* base (expmod base (- exp 1) m)) m)))) (define (list-expmod x exp m) (cond ((null? x) ()) ((not (pair? x)) (expmod x exp m)) (else (cons (list-expmod (car x) exp m) (list-expmod (cdr x) exp m))))) ;; Test Code: (display "--- STEP 2 TEST CASES ---") (newline) (display (list-expmod (list 2 6 4 8) 3 10))(newline); => (8 6 4 2) ( ;;;; ;;;; Step 3 - Using the Map Abstraction ;;;; ;(define (map proc items) ; (if (null? items) ; () ; (cons (proc (car items)) ;(map proc (cdr items))))) ; (define (list-expmod-2 item-list pow m) (map (lambda (x) (expmod x pow m)) item-list) ) ;; Test Code: (display "--- STEP 3 TEST CASES ---") (newline) (display (list-expmod-2 (list 2 6 4 8) 3 10))(newline); => (8 6 4 2)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lab9 - #| = LAB 9 = | | Author(s): | Lab Section: | |# ;...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online