#lang racket/base ;Exercise 1.1. Below is a sequence of expressions. What is the result printed b;y the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented. ;Exercise 1.1 ;outputs 10 10 ;outputs 12 (+ 5 3 4) ;outputs 8 (- 9 1) ;outputs 3 (/ 6 2) ;outputs 6 (+ (* 2 4) (- 4 6)) ;outputs a=3 (define a 3) ;outputs b=4 (define b (+ a 1)) ;outputs 19 (+ a b (* a b)) ;outputs False (= a b) ;outputs 4 (if (and (> b a) (< b (* a b))) b a) ;outputs 16 (cond ((= a 4) 6) ((= b 4) (+ 6 7 a)) (else 25)) ;outputs 6 (+ 2 (if (> b a) b a)) ;outputs 16 (* (cond ((> a b) a) ((< a b) b) (else -1)) (+ a 1)) ;Exercise 1.2 ;Translate the following expression into prefix form (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))) ;Exercise 1.3 ;Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. (define (square x) (* x x)) (define (TwoLargeSquares x y z) (cond ((and (< x y) ( < x z)) (+ (square y) (square z))) ((and (< y x) (< y z)) (+ (square x) (square z))) ((and (< z x) (< z y)) (+ (square x) (square y))))) (TwoLargeSquares 1 2 3) ;Exercise 1.4 ;Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure: (define (a-plus-abs-b a b) ((if (> b 0) + -) a b)) ;If b is greater than zero, then a adds with b, else, a subtract b resulting in a - - b (ie: a+b) ;Exercise 1.5 ;Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures: (define (p) (p)) (define (test x y) (if (= x 0) 0 y)) ; Then he evaluates the expression (test 0 (p)) ;What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.) ;ANSWER Applicative-order evaluation ;0, Applicative-order evaluation will never substitute in p for p, because it runs the if statement and determines x=0, resulting in 0. Opposite: using applicative-order evaluation, the evaluation of (test 0 (p)) never terminates, because (p) is infinitely expanded to itself… ;Normal-order evaluation ;0, Normal-order evaluation will replace p with p before running the if statement and determining p irrelevant. Opposite: using normal-order evaluation, the expression evaluates, step by step, to 0 ;**Normal order evaluates out->in (or left-> right) ;**Applicative order evaluates in->out (or right->left) ;Exercise 1.6 ; Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if: (define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) ; Delighted, Alyssa uses new-if to rewrite the square-root program: ;(define (sqrtiter guess x) ; (new-if (good-enough? guess x) ; guess ; (sqrtiter (improve guess x) ; x))) ;ANSWER: Undefined. The "new-if" is cond, which cannot iterate, and will test each set until it finds a "true" value; if it does not find a true condition, it will return "undefined." Wrong. The function continues calling itself without a return, overflowing the stack and causing an out of memory error. IE: "sqrt-iter calls itself (recursive) but never evaluates, causing an infinite call and memory overflow. ;Exercise 1.7 ;The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers? (define (sqrt x) (sqrtiter 1 x)) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (average x y) (/ (+ x y) 2)) (define (improve guess x) (average guess (/ x guess))) (define (sqrtiter guess x) (if (good-enough? guess x) guess (sqrtiter (improve guess x) x))) (sqrt 10000) (define (bettersqrt x) (bettersqrt-iter (* .1 x) x)) (define (bettersqrt-iter guess x) (if (< (abs (- guess (newguess guess x))) 0.001) guess (bettersqrt-iter (newguess guess x) x))) (define (newguess guess x) (average guess (/ x guess))) (bettersqrt 10000) ;Exercise 1.8. Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value (define (cubert x) (cubert-iter (* .1 x) x)) (define (cubert-iter guess x) (if (< (abs (- guess (cubenewguess guess x))) 0.001) guess (cubert-iter (cubenewguess guess x) x))) (define (cubenewguess guess x) (/ (+ (/ x (square guess)) (* 2 guess)) 3)) (cubert 1000) ;Exercise 1.9. Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1. ;PROCESS 1 ; (define (+ a b) ; (if (= a 0) ; b ; (inc (+ (dec a) b)))) ;Begin Expanding Process --- Recursive Process / Recursive Procedure ; inc (+ 3 5) ; inc inc (+ 2 5) ; inc inc inc (+ 1 5) ; inc inc inc inc (+ 0 5) ; returns 9 ;Process 2 ;(define (+ a b) ; (if (= a 0) ; b ; (+ (dec a) (inc b)))) ;Begin Expanding Process --- Iterative Process / Recursive Procedure ; (+ 3 6) ; (+ 2 7) ; (+ 1 8) ; (+ 0 9) Returns value 9 ; Exercise 1.10. The following procedure computes a mathematical function called Ackermann's function. (define (A x y) (cond ((= y 0) 0) ((= x 0) (* 2 y)) ((= y 1) 2) (else (A (- x 1) (A x (- y 1)))))) ;What are the values of the following expressions? (A 1 10) ;1024 ;(A 0 (A 1 9) ;(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (2)) (A 2 4) ;65536 (A 3 3) ;2^256 /// 65536 (16 bit interpreter IE: Max capability?) ;Consider the following procedures, where A is the procedure defined above: ;(define (f n) (A 0 n)) ;(f n) = 2*n ;(define (g n) (A 1 n)) ;(g n) = 2^n ;(define (h n) (A 2 n)) ;(h n) = 2^(2^n) ;(define (k n) (* 5 n n)) ;(k n) = 5(n^2) ;Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n. For example, (k n) computes 5n2. ;Exercise 1.11. A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process. ;Recursive Function (define (recursivefn n) (cond ((< n 3) n) (else (+ (recursivefn(- n 1)) (* 2 (recursivefn(- n 2))) (* 3 (recursivefn(- n 3))))))) (recursivefn 10) ;Iterative Function (define (iterativefn n) (if (< n 3) n (iterativefniter 3 n 2 2 0 4))) (define (iterativefniter count n firstoutput secondoutput thirdoutput answer) (cond ((= n 3) 4) ((< count n) (iterativefniter (+ 1 count) n answer (* 2 firstoutput) (* 3 (/ secondoutput 2)) (+ answer (* 2 firstoutput) (* 3 (/ secondoutput 2))))) (else answer))) (iterativefn 10) ;(+ (- count 1) (* 2 (- count 2)) (* 3 (- count 3)))))))) ; 3: firstoutput=2 secondoutput=2 thirdoutput=0 answer=4 ; 4: firstoutput=4(answer) secondoutput=4(2*lastfirstoutput) thirdoutput=3(3*lastsecondoutput/2) answer=11 ; 5: firstoutput=11(answer) secondoutput=2*4(2*answer 2 ago) thirdoutput=3*2(3*lastsecondoutput/2) answer=25 ; 6: firstoutput=25(answer) secondoutput=2*11(2*answer2ago=2*lastfirstoutput) thirdoutput=3*4(3*answer3ago=3*lastsecondoutput/2) answer=36 ; Exercise 1.12 (define (recursivepascal r c) (cond ((< r 3) 1) ((< c 2) 1) ((= c r) 1) (else (+ (recursivepascal (- r 1) c) (recursivepascal (- r 1) (- c 1)))))) (recursivepascal 5 3) ; Exercise 1.13 ; Prove that Fib(n) is the closest integer to n/5, where = (1 + 5)/2. Hint: Let = (1 - 5)/2. ; Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that Fib(n) = (n - n)/5. (define theta (/ (+ 1 (bettersqrt 5)) 2)) (define psi (/ (- 1 (bettersqrt 5)) 2)) (define (power x n) (cond ((< n 2) x) (else (poweriter x n x)))) (define (poweriter x n answer) (cond ((< n 2) answer) (else (poweriter x (- n 1) (* answer x))))) (define (fibn n) (/ (- (power theta n) (power psi n)) (bettersqrt 5))) (fibn 9) (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) (fib 9) ; Exercise 1.14 ; Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases? ; Function below (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc amount (- kinds-of-coins 1)) (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50))) (cc 100 5) ; Exercise 1.15 ;The sine of an angle (specified in radians) can be computed by making use of the approximation sin x x if x is sufficiently small, and the trigonometric identity ; to reduce the size of the argument of sin. (For purposes of this exercise an angle is considered ``sufficiently small'' if its magnitude is not greater than 0.1 radians.) These ideas are incorporated in the following procedures: ;(define (cube x) (* x x x)) ;(define (p x) (- (* 3 x) (* 4 (cube x)))) ;(define (sine angle) ; (if (not (> (abs angle) 0.1)) ; angle ; (p (sine (/ angle 3.0))))) ; a. How many times is the procedure p applied when (sine 12.15) is evaluated? ; 5 times ; b. What is the order of growth in space and number of steps (as a function of a) used by the process generated by the sine procedure when (sine a) is evaluated? ; O(a) steps ; O(a) memory Wrong -- Logarithmic growth ;Exercise 1.16. ;Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. ;(Hint: Using the observation that (bn/2)2 = (b2)n/2, keep, along with the exponent n and the base b, an additional state variable a, and define the state transformation in such a way that the product a bn is unchanged from state to state. ;At the beginning of the process a is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.) (define (fastpower x n) (cond ((< n 2) x) (else (fastpoweriter x n 1)))) (define (fastpoweriter x n answer) (cond ((< n 2) answer) ((even? n) (fastpoweriter x (/ n 2) (* (square x) answer))) (else (* x (fastpoweriter x (/ (- n 1) 2) (* (square x) answer)))))) (define (even? n) (integer? (/ n 2))) (fastpower 5 5) ;Exercise 1.17. ;The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. ;In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication procedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure: ;(define (* a b) ; (if (= b 0) ; 0 ; (+ a (* a (- b 1))))) ;This algorithm takes a number of steps that is linear in b. Now suppose we include, together with addition, operations double, which doubles an integer, and halve, which divides an (even) integer by 2. ;Using these, design a multiplication procedure analogous to fast-expt that uses a logarithmic number of steps. (define (fastmultiply a b) (cond ((= b 0) 0) (else (fastmultiplyiter a b a)))) (define (fastmultiplyiter a b answer) (cond ((= b 1) answer) ((= b 0) 0) ((even? b) (fastmultiplyiter a (halve b) (double answer))) (else (- (fastmultiplyiter a (halve (+ b 1)) (double answer)) answer)))) (define (double a) (cond ((= a 0) 0) (else (+ a a)))) (define (halve a) (cond ((= a 0) 0) (else (/ a 2)))) (fastmultiply 4 5) ;Exercise 1.18 ;See solution for 1.17... ;Exercise 1.19 (define (fibfib n) (fib-iter 1 0 0 1 n)) (define (fib-iter a b p q count) (cond ((= count 0) b) ((even? count) (fib-iter a b (+ (square p) (square q)) ; compute p' (+ (* 2 p q) (square q)) ; compute q' (/ count 2))) (else (fib-iter (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) p q (- count 1))))) (fibfib 3) ;Alternate Solution Exercise 1.19 (define (fibfibfib n) (fibfibfib-iter 1 0 n 1)) (define (fibfibfib-iter a b count counter) (cond ((= count counter) a) ((<= counter (/ count 2)) (fibfibfib-iter (+ (* 2 a b) (square a)) (+ (square b) (square a)) count (* counter 2))) (else (fibfibfib-iter (+ b a) a count (+ counter 1))))) (fibfibfib 12) ; Exercise 1.20 ; Exercise 1.20 ; The process that a procedure generates is of course dependent on the rules used by the interpreter. ; As an example, consider the iterative gcd procedure given above. ; Suppose we were to interpret this procedure using normal-order evaluation, as discussed in section 1.1.5. (The normal-order-evaluation rule for if is described in exercise 1.5.) ; Using the substitution method (for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the remainder operations that are actually performed. ; Euclid's Algorithm (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) ; (gcd 206 40) ; (gcd 40 (remainder 206 40) ; (gcd 40 6) ; (gcd 6 (remainder 40 6) ; (gcd 6 4) ; (gcd 4 (remainder 6 4) ; (gcd 4 2) ; (gcd 2 (remainder 4 2) ; (gcd 2 0) ; returns a = 2 ; How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? ; 18 remainder operations are performed in normal-order evaluation. ; In the applicative-order evaluation? ; 4 remainder operations are performed in applicative-order evaluation ;Exercise 1.21. Use the smallest-divisor procedure to find the smallest divisor of each of the following numbers: 199, 1999, 19999. ;(define (smallest-divisor n) ; (find-divisor n 2)) ;(define (find-divisor n test-divisor) ; (cond ((> (square test-divisor) n) n) ; ((divides? test-divisor n) test-divisor) ; (else (find-divisor n (+ test-divisor 1))))) ;(define (divides? a b) ; (= (remainder b a) 0)) (define (next n) (if (= n 2) 3 (+ n 2))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (next test-divisor))))) (define (divides? a b) (= (remainder b a) 0)) (smallest-divisor 199) (smallest-divisor 1999) (smallest-divisor 19999) ; Exercise 1.22 ;Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). ;The following timed-prime-test procedure, when called with an integer n, prints n and checks to see if n is prime. ;If n is prime, the procedure prints three asterisks followed by the amount of time used in performing the test. ;(define (timed-prime-test n) ; (start-prime-test n (current-milliseconds))) ;(define (start-prime-test n start-time) ; (cond ((prime? n) ;(report-prime (- (current-milliseconds) start-time))) ; (else (prime? (+ n 2))))) ;(define (report-prime elapsed-time) ; (display " *** ") ; (display elapsed-time)) (define (prime? n) (if (= n (smallest-divisor n)) n 0)) ;(timed-prime-test 3485749865) (define (search-for-primes n) (if (= (prime? n) n) n (search-for-primes (+ n 2)))) ;(prime? 9) (search-for-primes 100021) ; (if ((= (timed-prime-test start)) #f (search-for-primes (+ start 2) start 0 0)) ; (search-for-primes (+ start 2) 0)) ;Using this procedure, write a procedure search-for-primes that checks the primality of consecutive odd integers in a specified range. Use your procedure to find the three smallest primes larger than 1000; larger than 10,000; larger than 100,000; larger than 1,000,000. ;Runtime function not available on racket. ;1,000 primes: 1,009; 1,013; 1,019; 10,000 primes: 10,007; 10,009; 10,0037; 100,000 primes: 100,003; 100,019; 100,043 ;Note the time needed to test each prime. Since the testing algorithm has order of growth of (n), you should expect that testing for primes around 10,000 should take about 10 times as long as testing for primes around 1000. ;Do your timing data bear this out? How well do the data for 100,000 and 1,000,000 support the n prediction? ;Is your result compatible with the notion that programs on your machine run in time proportional to the number of steps required for the computation? ;Exercise 1.23 ;The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. ;This suggests that the values used for test-divisor should not be 2, 3, 4, 5, 6, ..., but rather 2, 3, 5, 7, 9, .... To implement this change, define a procedure next that returns 3 if its input is equal to 2 and otherwise returns its input plus 2. ;Modify the smallest-divisor procedure to use (next test-divisor) instead of (+ test-divisor 1). With timed-prime-test incorporating this modified version of smallest-divisor, run the test for each of the 12 primes found in exercise 1.22. ;Since this modification halves the number of test steps, you should expect it to run about twice as fast. Is this expectation confirmed? ;If not, what is the observed ratio of the speeds of the two algorithms, and how do you explain the fact that it is different from 2? ;see exercise 1.21 ;Exercise 1.24 ;skip--runtime doesnt work (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (if (or (= (square (expmod base (/ exp 2) m)) 1) (= (square (expmod base (/ exp 2) m)) (- exp 1))) (remainder (square (expmod base (/ exp 2) m)) m) (display "not a prime"))) (else (remainder (* base (expmod base (- exp 1) m)) m)))) ;(define (fast-expt b n) ; (cond ((= n 0) 1) ; ((even? n) (square (fast-expt b (/ n 2)))) ; (else (* b (fast-expt b (- n 1)))))) ;Exercise 1.25 ;Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod. ;After all, she says, since we already know how to compute exponentials, we could have simply written ;(define (expmod base exp m) ; (remainder (fast-expt base exp) m)) ;Is she correct? Would this procedure serve as well for our fast prime tester? Explain. ; Her procedure works but is inefficient with higher numbers. Her process only takes the remainder once (at the end). ; Although there are fewer process steps, the computer takes longer to find a remainder when the number becomes so large, so it is ultimately slower. ; Exercise 1.26 ; By using * instead of (square) he executes (expmod base (/ exp 2) m) which doubles when called because it is recursive. ; By using square, he reduces the process in half. expmod calls itself once per iteration, and then squares. ; Exercise 1.27 (define (fermat-test n) (define (try-it a) (= (expmod a n n) a)) (try-it (+ 1 (random (- n 1))))) ;(fermat-test 561) (smallest-divisor 561) ; Exercise 1.28 (define (miller-rabin-test n) (define (try-it a) (= (expmod a n n) a)) (try-it (+ 1 (random (- n 1))))) ;(miller-rabin-test 100) ; (expmod) function not working..... ; Exercise 1.29 (define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx)) (define (cube x) (* x x x)) (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (simpson f a b n) (define h (/ (- b a) n)) (define (nextcountbytwo a) (+ a (* h 2))) (* (/ h 3) (+ (f (+ a (* h 0))) (f (+ a (* h n))) #! first and last term (* 4 (sum f (+ a (* h 1)) nextcountbytwo b)) #! odd terms (* 2 (sum f (+ a (* h 2)) nextcountbytwo b))))) #! even terms (simpson cube 0 1 1000) (integral cube 0 1 .001) ; Exercise 1.30 (define (inc n) (+ n 1)) (define (newsum func a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ result (func a))))) (iter a 0)) (newsum cube 1 inc 10) ; Exercise 1.31 (define (product-iterative func a next b) (define (iter a result) (if (> a b) result (iter (next a) (* result (func a))))) (iter a 1)) (define (product-recursive func a next b) (if (> a b) 1 (* (func a) (product-recursive func (next a) next b)))) (product-iterative cube 1 inc 3) (product-recursive cube 1 inc 3) ; Exercise 1.32 (define (accumiter combiner null-value func a nexxt b) (define (accumsubloop aa result) (if (> aa b) result (accumsubloop (nexxt aa) (combiner result (func aa))))) (accumsubloop a null-value)) (accumiter * 1 cube 1 inc 3) (define (accumrecur combiner null-value func a nexxt b) (if (> a b) null-value (combiner (func a) (accumrecur combiner null-value func (nexxt a) nexxt b)))) (accumrecur * 1 cube 1 inc 3) ;Exercise 1.33 ;(define (prime? n) ; (if (= n (smallest-divisor n)) ; n ; 0)) (prime? 7) (define (filtered-accumulate combiner null-value func a nexxt b filter) (cond ((= (filter a) 0) (filtered-accumulate combiner null-value func (nexxt a) nexxt b filter)) ((and (= (filter a) a) (<= a b)) (combiner (func a) (filtered-accumulate combiner null-value func (nexxt a) nexxt b filter))) (else null-value))) (filtered-accumulate + 0 square 1 inc 5 prime?) (define (filtered-product-gcds combiner null-value func a nexxt b filter) (cond ((not (= (filter a b) 1)) (filtered-product-gcds combiner null-value func (nexxt a) nexxt b filter)) ((and (= (filter a b) 1) (<= a b)) (combiner a (filtered-product-gcds combiner null-value func (nexxt a) nexxt b filter))) (else null-value))) (filtered-product-gcds * 1 sum 1 inc 5 gcd) ;Exercise 1.34 What happens if we (perversely) ask the interpreter to evaluate the combination (f f)? Explain. ; (f f) -> (f 2) -> (2 2) -> error 2 is not a procedure ;Exercise 1.35 Show that the golden ratio (section 1.2.2) is a fixed point of the transformation x 1 + 1/x, and use this fact to compute by means of the fixed-point procedure. (define tolerance 0.00001) (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (newline) (display guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess)) ; proof x = 1 + 1/x plug in golden ratio for x and solve. (fixed-point (lambda (y) (+ 1 (/ 1 y))) 1.0) ;Exercise 1.36 modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in exercise 1.22. ;Then find a solution to xx = 1000 by finding a fixed point of x log(1000)/log(x). ;(Use Scheme's primitive log procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. ;(Note that you cannot start fixed-point with a guess of 1, as this would cause division by log(1) = 0.) ;without damping 30+ iterations (fixed-point (lambda (x) (/ (log 1000) (log x))) 10) ;with damping 11 iterations (fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) 10) ;Exercise 1.37 (define (cont-frac n d k) (if (= k 0) 0 (/ (n k) (+ (d k) (cont-frac n d (- k 1)))))) (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 50)
Write, Run & Share Racket code online using OneCompiler's Racket online compiler for free. It's one of the robust, feature-rich online compilers for Racket language, running on the latest version 6.8. Getting started with the OneCompiler's Racket compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Racket
and start coding.
OneCompiler's Racket online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Racket program which takes name as input and print your name with hello.
#lang racket/base
(define name (read))
(printf "Hello ~a.\n" name)
Racket is a general-purpose programming language based on the Scheme dialect of Lisp. It is also used for scripting, computer science education, and research related applications.
Item | Decsription |
---|---|
; | To comment a single line |
;; | to mark important comments |
#; | to comment the following s-expression |
Data-type | Decsription |
---|---|
Numbers | represents integers, float and complex numbers |
Boolean | #t and #f are the two boolean literals |
Strings | To represent sequence of characters and double quotes("") are used to represent strings |
let and define are used to declare variables
(let ([id value-expression] ...) body ...+)
(let proc-id ([id init-expression] ...) body ...+)
define id expression
> (let ([x 10]) x)
10
If, If-else are used when you want to perform a certain set of operations based on conditional expressions.
(if cond-expr then-expr)
(if cond-expr then-expr else-expr)
For loop is used to iterate a set of statements based on a condition.
(for (for-clause ...) body-or-break ... body)
where
for-clause = [id seq-expr] | [(id ...) seq-expr] | #:when guard-expr | #:unless guard-expr | break-clause
break-clause = #:break guard-expr | #:final guard-expr
body-or-break = body | break-clause
seq-expr : sequence?
A lambda expression is used to create a function.
(lambda (argument-id ...)
body ...+)