;1. (sqrt-iter(improve guess x) x) will be evaluated before any execution in new-if. ; That is, causing an infinite loop because no matter (goodenough? guess x) returns ; true or false, (sqrt-iter (improve guess) x) executes before the test. ;2. (define (switch arg) (cond ((empty? arg) '()) ((member? (first arg) '(You you)) (se 'i (switchnext (bf arg)))) (else (switchnext arg)))) (define (check wd) (cond ((equal? wd 'you) 'me) ((member? wd '(me I)) 'you) (else wd))) (define (switchnext arg) (if (empty? arg) '() (se (check (first arg)) (switchnext (bf arg))))) ;STk> (load "home1.scm") ;STk> (switch '(You told me that I should wake you up)) ;(i told you that you should wake me up) ;STk> (switch '(you)) ;(i) ;STk> (switch '(me)) ;(you) ;STk> (switch '(how are you)) ;(how are me) ;STk> (switch '()) ;() ;3. (define (squares arg) (if (empty? arg) '() (se (square (first arg)) (squares (bf arg))))) (define (square x) (* x x)) ;STk> (squares '(1 2 3 4 5)) ;(1 4 9 16 25) ;STk> (squares '()) ;() ;STk> (squares '(0 0 222)) ;(0 0 49284) ;STk> (squares '(11.2 12.2 4.0)) ;(125.44 148.84 16.0) ;4. (define (ordered? arg) (cond ((empty? arg) #t) ((empty? (bf arg)) #t) ((> (first arg) (first (bf arg))) #f) (else (ordered? (bf arg))))) ;STk> (ordered? '(1 2 3 4 5)) ;#t ;STk> (ordered? '(1 1 1 2 3 4 5)) ;#t ;STk> (ordered? '(2 1 3 4 6 5)) ;#f ;5. (define (ends-e arg) (if (empty? arg) '() (se (check-e (first arg)) (ends-e (bf arg))))) (define (check-e wd) (if (member? (last wd) '(e E)) wd '())) ;STk> (ends-e '(please put the salami above the blue elephant)) ;(please the above the blue) ;STk> (ends-e '(no e-ends in this test)) ;() ;STk> (ends-e '()) ;() ;6. (define (out-and x) (print '(and is ordinary)) (= x 0)) (define (out-or x) (print '(or is ordinary)) (= x 0)) ;STk> (and (= 1 0) (out-and 0)) ;#f ;STk> (or (= 1 1) (out-or 1)) ;#t ;STk> (out-and 0) ;(and is ordinary) ;#t ;STk> (out-or 1) ;(or is ordinary) ;#f ;As the examples showing above on #6. If (out-and 0), (out-or 1) get called, they ;will print an aditional message, but when we use the and, or, we didn't see the ;message. The we conclude that (out-and 0) and (out-or 1) is not called, which ;means that and, or are special form, not ordinary procedure. ;Advantage of special form is that it saves computing time, which skips the ;unnecessary steps. The advantage of ordinary procedure is we can combine fuctions, ;such as (out-and x) into the procedure and make sure it's called to generate some ;side-effects, such as printing a message.