• SICP习题 1.6 (再探 函数 与 正则序 应用序 关系)


    <一>

    (define (new-if predicate then-clause else-clause)
    (cond (predicate then-clause)
    (else else-clause)))

    (new-if (= 2 3) 0 5)
    (new-if (= 1 1) 0 5)

    new-if在两种环境下均运行正常.

    (if (= 2 3) 0 5)
    (if (= 1 1) 0 5)

    if 在R5RS下输出正常,在Lazy Racket下为(delay ...)

    <二>

    (define (new-if predicate then-clause else-clause)
    (cond (predicate then-clause)
    (else else-clause)))

    (define (sqrt-iter guess x)
    (new-if (good-enough? guess x)
    guess
    (sqrt-iter (improve guess x)
    x)))

    (define (improve guess x)
    (average guess (/ x guess)))

    (define (average x y)
    (/ (+ x y) 2))

    (define (good-enough? guess x)
    (< (abs (- (square guess) x)) 0.001))

    (define (sqrta x)
    (sqrt-iter 1.0 x))

    (define (square x)
    (* x x))

    (sqrta 9)

    在R5RS下内存被耗尽,而在Lazy Racket下正常输出.

    原因是new-if在R5RS,也就是应用序下,传入的每一个分支都会被求值.

    <三>

    (define (new-if predicate then-clause else-clause)
    (cond (predicate then-clause)
    (else else-clause)))

    (new-if #t (display "good") (display "bad"))

     R5RS输出:goodbad

     Lazy Racket输出:good

  • 相关阅读:
    docker articles&videos
    Docker Resources
    IL-rewriting profiler
    memory dump and CLR Inside Out
    tcp/ip basics
    What Every CLR Developer Must Know Before Writing Code
    CLR profiler
    Debug CLR and mscorlib
    JIT Compiler
    calling into the CLR from managed code via QCall and FCall methods
  • 原文地址:https://www.cnblogs.com/R4mble/p/7878755.html
Copyright © 2020-2023  润新知