您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 计算机程序的构造与解释答案
1.16(define(fast-expt-iterabn)(define(squarex)(*xx))(cond((=n0)a)((even?n)(fast-expt-itera(squareb)(/n2)))((odd?n)(fast-expt-iter(*ab)b(-n1)))))1.17(define(halvex)(/x2))(define(doublex)(+xx));递归(define(fast-addbn)(cond((=n0)0)((even?n)(double(fast-addb(halven))))(else(+b(fast-addb(-n1))))));迭代(define(fast-add-iterabn)(cond((=n0)a)((even?n)(fast-add-itera(doubleb)(halven)))(else(fast-add-iter(+ab)b(-n1)))))(fast-add37)(fast-add-iter059)1.19(define(fibn)(fib-iter1001n))(define(fib-iterabpqcount)(cond((=count0)b)((even?count)(fib-iterab(+(*qq)(*pp))(+(*qq)(*2pq))(/count2)))(else(fib-iter(+(*bq)(*aq)(*ap))(+(*bp)(*aq))pq(-count1)))))(fib10)1.22我用PLTScheme编译环境的,没有runtime1.29(define(sumtermanextb)(if(ab)0(+(terma)(sumterm(nexta)nextb))))(define(simpsonfabn)(defineh(/(-ba)n))(define(term-fk)(f(+a(*kh))))(define(termk)(cond((or(=k0)(=kn))(term-fk))((even?k)(*2(term-fk)))(else(*4(term-fk)))))(define(nextx)(+x1))(*h(/(sumterm0nextn)3)))(define(cubex)(*xxx));1/4(simpsoncube011000)1.30(define(sumtermanextb)(define(iteraresult)(if(ab)result(iter(nexta)(+(terma)result))))(itera0))(define(squarex)(*xx))(define(nextx)(+x1))(sumsquare0next3)1.31;递归乘法(define(producttermanextb)(if(ab)1(*(terma)(productterm(nexta)nextb))));迭代乘法(define(product-itertermanextb)(define(iteraresult)(if(ab)result(iter(nexta)(*(terma)result))))(itera1))(define(squarex)(*xx))(define(nextx)(+x1));36(productsquare1next3);576(product-itersquare1next4);factorial(define(factorialn)(define(identityx)x)(productidentity1nextn));24(factorial4);下面用4.0的话就可以把原来是显示成分数的结果显示成小数了(define(pin)(define(next-2x)(+x2))(define(partx)(/(*x(+x2))(*(+x1)(+x1))))(*4.0(productpart2next-2(*n2))))(pi1000)1.32(define(accumulatecombinernull-valuetermanextb)(if(ab)null-value(combiner(terma)(accumulatecombinernull-valueterm(nexta)nextb))))(define(accumulate-itercombinernull-valuetermanextb)(define(iteraresult)(if(ab)result(iter(nexta)(combiner(terma)result))))(iteranull-value))(define(sumtermanextb)(define(addab)(+ab))(accumulateaddtermanextb))(define(prodecttermanextb)(define(multab)(*ab))(accumulatemulttermanextb))1.33(define(filtered-accumulatefilterscombinernull-valuetermanextb)(if(ab)null-value(if(filtersab)(combiner(terma)(filtered-accumulatefilterscombinernull-valueterm(nexta)nextb))(filtered-accumulatefilterscombinernull-valueterm(nexta)nextb))))(define(filtered-accumulate-iterfilterscombinernull-valuetermanextb)(define(iteraresult)(if(ab)result(if(filtersab)(iter(nexta)(combiner(terma)result))(iter(nexta)result))))(iteranull-value))(define(combiner-addab)(+ab))(define(combiner-multab)(*ab))(define(next-1x)(+x1))(define(smallest-divisorn)(find-divisorn2))(define(find-divisorntest-divisor)(define(squarex)(*xx))(cond(((squaretest-divisor)n)n)((divides?test-divisorn)test-divisor)(else(find-divisorn(+test-divisor1)))))(define(divides?ab)(=(remainderba)0))(define(prime?nlast)(=n(smallest-divisorn)))(define(identityx)x);18求1-10中所有素数平方之和(filtered-accumulateprime?combiner-add0identity1next-110)(define(gcd-filterin)(and(in)(=(gcdni)1)));189小于n的所有与n互素的正整数的乘积(filtered-accumulategcd-filtercombiner-mult1identity1next-110);189(filtered-accumulate-itergcd-filtercombiner-mult1identity1next-110)1.34(define(fg)(g2))(f(lambda(z)(*z(+z1))));替换成(22),而前一个2不能代表为过程procedure,因此无法执行(ff)2011-02-2322:05:22回应第52页1.35(definetolerance0.00001)(define(fixed-pointffirst-guess)(define(close-enough?v1v2)((abs(-v1v2))tolerance))(define(tryguess)(let((next(fguess)))(if(close-enough?guessnext)next(trynext))))(tryfirst-guess))(define(golden-point)(fixed-point(lambda(x)(+1(/1x)))1.0));为什么要取倒数?(/1(golden-point))1.37-1.39(define(cont-frac-iterndk)(define(tryguesst)(let((next(/(nt)(+(dt)guess))))(if(=t0)next(trynext(-t1)))))(try(/(nk)(dk))(-k1)))(define(cont-fracndk)(define(tryt)(if(=tk)(/(nt)(dk))(/(nt)(+(dt)(try(+t1))))))(try1))(define(cont-frac-accumulatecombinerndk)(define(tryguesst)(let((next(/(nt)(combiner(dt)guess))))(if(=t0)next(trynext(-t1)))))(try(/(nk)(dk))(-k1)));容易知道所求为0.6180339887498948,而当k=10时最接近(cont-frac(lambda(i)1.0)(lambda(i)1.0)10);1.38(define(d-ex)(if(=(remainder(+x1)3)0)(*2.0(/(+x1)3))1))(define(ek)(+2(cont-frac-iter(lambda(i)1.0)d-ek)));算出来的e误差很大啊等于2.58197670(e1000);1.39(define(tan-cfxk)(define(n-tank)(if(=k1)x(-(*xx))));之前想着抽象cont-frac中的combiner,没想到直接加一个负号(define(d-tank)(-(*k2)1))(cont-fracn-tand-tank))(tan-cf(/3.141593)10)1.40(definetolerance0.00001)(define(fixed-pointffirst-guess)(define(close-enough?v1v2)((abs(-v1v2))tolerance))(define(tryguess)(let((next(fguess)))(if(close-enough?guessnext)next(trynext))))(tryfirst-guess))(definedx0.00001)(define(derivg)(lambda(x)(/(-(g(+xdx))(gx))dx)))(define(newton-transformg)(lambda(x)(-x(/(gx)((derivg)x)))))(define(newtons-methodgguess)(fixed-point(newton-transformg)guess))(define(cubicabc)(lambda(x)(+(*xxx)(*axx)(*bx)c)))(newtons-method(cubic210)1)1.41(define(incx)(+x1))(define(doublef)(lambda(x)(f(fx))))((doubleinc)8)(((double(doubledouble))inc)5);上式相当与下式,即执行了2^n次,n为double的个数((double(double(double(doubleinc))))5)1.42(define(composefg)(lambda
本文标题:计算机程序的构造与解释答案
链接地址:https://www.777doc.com/doc-5535726 .html