zoukankan      html  css  js  c++  java
  • SICP 1.21 1.22 体会

    1.21 简单的将书上代码敲了一遍。 非常顺利就过了。

    1.22 就悲剧了。

             先按书本的意思。代码非常快就写完了。但计算的时间在机子上漂浮不定。 3-5倍之间。

             代码例如以下:

    (define (search-for-primes start end count)
        (define (timed-prime-test n)
    (newline)
            (display n)
    (start-prime-test n (runtime)))
        (define (start-prime-test n start-time)
    (if (prime? n)
       (report-prime (- (runtime) start-time))
       0))
        (define (report-prime elapsed-time)
    (display " *** ")
            (display elapsed-time)
    1)
        (define (prime? n)
    (= n (smallest-divisor n)))
        (define (search-iter start end count)
    (if (or (> start end) (= count 0))
       0
       (if (= (timed-prime-test start) 1)
    (search-iter (+ start 1) end (- count 1))
    (search-iter (+ start 1) end count))))
        (search-iter start end count))


          问题就悲剧在。我想换一种想法。

         一般常见的project測试代码都是例如以下风格:

    记录開始时间

            调用函数

            计算结束时间

        于是,我非常悲剧地写了以下代码

       (print-test-time   (runtime)  (search-iter start end count)  (runtime))

       却惊奇的发现前后2次计算的时间竟然全然一样。

       再认真将书从头看了一遍, 我晕, 这本书最重要的东西竟然没讲。 函数的參数究竟按什么顺序求的?

      google了一下,晕, 竟然是依赖编译器确定。

    也就是说不知道。

      对于一般状态无关的參数的确没有问题。 但对于时间依赖的參数就悲剧了。

      再回过头来思考计算模型, 发现这本书更大的问题。

    对什么东西能做操作符,什么东西能做操作数竟然全然没有给出清晰的定义。

      1和1.0, 1/1, true, 究竟是不是一回事,书本也全然没讲清楚。

      作为讲编程语言的书来说。 这本书真是一塌糊涂。

     上面的函数 (timed-prime-test n) 有3个语句, 概念进入得非常突入, 这3个语句运行按什么顺序呢? 返回值是哪个呢? 基本上靠读者去猜。

    (本人猜的是从上往下运行,返回最后一个语句 ,可是不是程序确实是这样还不大自信)

    (newline)
            (display n)
    (start-prime-test n (runtime)))

  • 相关阅读:
    限制泛型可用类型,类型通配符声明,泛型方法
    泛型简介,泛型类及使用
    异常概念和处理机制,try-catch-finally,throw和throws,自定义异常
    随机验证码
    常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类
    String、StringBuffer和StringBuilder,定义一个自己的StringBuilder的类
    自动装箱和拆箱,枚举类型
    使用内部类开发一个存放数据的容器
    手推期望、方差
    ML 徒手系列 最大似然估计
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6766782.html
Copyright © 2011-2022 走看看