ANSWERS: Problem Set 1, CS 257

  1. Get Emacs and STk working for you. Demonstrate it by calculating the sin of 1E100 (ten to the power one hundred).
    ;;; sin(10^100)
    (sin (expt 10 100))
    
    ;;; -0.567656625438599
    
    ;;; note that if the above is accurate, the following should all be different:
    
    (sin (expt 10 100))
    (sin (+ (expt 10 100) 1))
    (sin (+ (expt 10 100) 2))
    (sin (+ (expt 10 100) 3))
    
    ;;; but they all evaluate to the same thing.  So we certainly cannot
    ;;; trust the above results!
    

    (Extra credit: do you think your answer is numerically correct? Conduct an experiment to check.)

  2. Use Scheme to calculate the average of: 1, 1, 2, 3, 5, 8, 13, 21, 33.
    (/ (+ 1 1 2 3 5 8 13 21 33.0) 9)
    ;;; 9.6666666666666661
    

  3. format and indent the following properly:
    (+ (* (+ 1 (* (+ 1 (* (+ 1 (* (+ 1 (+ 1 1)) (* (+ 1 1) (+ 1 (+ 1
    1))))) (+ 1 (* (+ 1 (+ 1 1)) (* (+ 1 1) (+ 1 (+ 1 1))))))) (+ 1 (* (+
    1 (* (+ 1 (+ 1 1)) (* (+ 1 1) (+ 1 (+ 1 1))))) (+ 1 (* (+ 1 (+ 1 1))
    (* (+ 1 1) (+ 1 (+ 1 1))))))))) (+ 1 (* (+ 1 (* (+ 1 (* (+ 1 (+ 1 1))
    (* (+ 1 1) (+ 1 (+ 1 1))))) (+ 1 (* (+ 1 (+ 1 1)) (* (+ 1 1) (+ 1 (+ 1
    1))))))) (+ 1 (* (+ 1 (* (+ 1 (+ 1 1)) (* (+ 1 1) (+ 1 (+ 1 1))))) (+
    1 (* (+ 1 (+ 1 1)) (* (+ 1 1) (+ 1 (+ 1 1)))))))))) 1)
    

    This is not too hard for me to read (after all, human readability is the point of proper indentation!)

    (+ (* (+ 1 (* (+ 1 (* (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
                                     (+ 1 (+ 1 1)))))
    		      (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			      (+ 1 (+ 1 1)))))))
    	      (+ 1 (* (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			         (+ 1 (+ 1 1)))))
    		      (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			         (+ 1 (+ 1 1)))))))))
          (+ 1 (* (+ 1 (* (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			         (+ 1 (+ 1 1)))))
    		      (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			         (+ 1 (+ 1 1)))))))
    	      (+ 1 (* (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			         (+ 1 (+ 1 1)))))
    		      (+ 1 (* (+ 1 (+ 1 1))
    			      (* (+ 1 1)
    			         (+ 1 (+ 1 1)))))))))) 
       1)
    

  4. Make a very complicated expression that expresses a calculation of interest to you and indent it properly.

    If faculty raises continue for the next five years as they did this year, the following is how much I will make in 2002 for each dollar I made this year:

    (* 1 1 1 1 1)
    
    More seriously, here were a few good ones:
    ;;; Chris Davis (chris2d@unm.edu
    
    ;;; This will figure out the average time I spend in the ESC pod,
    ;;; per day.  Everything is in hours.
    
    (/ (+ (+ (- 9 7.5) (- 5 2))        ; Monday 7:30 to 9, 2pm to 5pm
          (+ (- 9.5 7.5) (- 8 5))      ; Tues 7:30 to 9:30, 5pm to 8pm
          (+ (- 9 7.5) (+ 1 ) (- 7 3)) ; Wed. 7:30 to 9, 12 to 1, 3 to 7pm
          (+ (- 12.5 7.5) (- 5 4))     ; Thurs. 7:30 to noon, 4pm to 5pm
          (+ (- 9 7.5) (- 5 2))        ; Friday 7:30 to 9, 2pm to 5
          (+ 1.5)                      ; Sat.: usually about 1.5 hours
          (+ 5))                       ; Sun. usually about 5 hours
       7)                              ; divide by number of days in a week
    
    
    
    ;;; Justin Smith (jthsmith@unm.edu)
    
    ;;; 4. The Drake Equation to estimate the number of communicating
    ;;; technological civilizations in the Milky Way galaxy:
    ;;; N = N* * Fs * Np * Fe * Fl * Fi * (L / Lmw)
    
    ;;; N = number of technological civilizations in the Milky Way
    ;;; N* = number of stars in the galaxy
    ;;; Fs = fraction of sunlike stars
    ;;; Np = average number of planets per star
    ;;; Fe = fraction of planets suitable for life
    ;;; Fl = fraction of planets where life actually develops
    ;;; Fi = fraction of planets where intelligent civilizations arise
    ;;; L = lifetime of civilization with ability to communicate
    ;;; Lmw = lifetime of the Milky Way galaxy
    
    ;;; I'll use: N* = 3E11, Fs = 0.3, Np = 10, Fe = 0.025, Fl = 0.75. Fi = 0.01,
    ;;;           L = 10000, Lmw = 10E10
    
    (* 3E11 (* 0.3 (* 10 (* 0.025 (* 0.75 (* 0.01 (/ 10000 10E10)))))))
    
    
    
    
    ;;; Andrew E Claiborne aeclai@unm.edu
    
    ;;; This calculates the spring rate for a coil spring with 8 coils, made
    ;;; out of 1 inch diameter wire, formed in a 4.3 inch diameter coil
    
    (/ (expt (* 1406250 1) 4)        ; (1406250 * wire diameter)^4
       (* 8                          ; number of coils
          (expt (- 4.3 1) 3)))       ; (coil diameter - wire diameter)^3
    
    
    
    
    
    ;;; Jamin C. S. Ragle jragle@unm.edu
    
    ;;; An approximation of pi/4 by power series.
    
    (+ (/ 1 1)
       (- (/ 1 3))
       (/ 1 5)
       (- (/ 1 7))
       (/ 1 9)
       (- (/ 1 11))
       (/ 1 13)
       (- (/ 1 15))
       (/ 1 17)
       (- (/ 1 19))
       (/ 1 21)
       (- (/ 1 23))
       (/ 1 25)
       (- (/ 1 27))
       (/ 1 29))
    

  5. Draw a big drawing using the turtle. Just edit a file to have all the commands in it, and draw the thing by loading the file into stk. E-mail the contents of this file as part of your homework. (You can compose the e-mail within emacs using C-x m.)

    Make the drawing really complicated and cute. This is a good way to learn the cut-and-paste features of emacs, and maybe query-replace too. By using these, you should be able to eg easily spell your name in big letters.

    (write "Ask your friends, there were lots of good ones!")
    

Barak Pearlmutter <bap@cs.unm.edu>