zoukankan      html  css  js  c++  java
  • SICP_3.5-3.6

      1 (#%require (only racket/base random))
      2 
      3 ;;;;;;;;;3.5
      4 (define (estimate-integral expriment x1 x2 y1 y2 trials)
      5   (let ((length (abs (- x2 x1)))
      6         (high (abs (- y2 y1))))
      7     (* (* high length) (monte-carlo trials expriment))))
      8 
      9 (define (monte-carlo trials expriment)
     10   (define (iter trials-remaining trials-passed)
     11     (cond ((= trials-remaining 0)
     12           (/ trials-passed trials))
     13           ((expriment)
     14            (iter (- trials-remaining 1) (+ trials-passed 1)))
     15           (else
     16            (iter (- trials-remaining 1) trials-passed))))
     17   (iter trials 0))
     18 
     19 ;(define p1
     20 ;  (let ((x (random-in-range -1 1))
     21 ;        (y (random-in-range -1 1)))
     22 ;    (<= (+ (square x) (square y)) 1)))
     23 (#%require (only racket/base random))
     24 
     25 ;;;;;;;;;3.5
     26 (define (estimate-integral expriment x1 x2 y1 y2 trials)
     27   (let ((length (abs (- x2 x1)))
     28         (high (abs (- y2 y1))))
     29     (* (* high length) (monte-carlo trials expriment))))
     30 
     31 (define (monte-carlo trials expriment)
     32   (define (iter trials-remaining trials-passed)
     33     (cond ((= trials-remaining 0)
     34           (/ trials-passed trials))
     35           ((expriment)
     36            (iter (- trials-remaining 1) (+ trials-passed 1)))
     37           (else
     38            (iter (- trials-remaining 1) trials-passed))))
     39   (iter trials 0))
     40 
     41 ;(define p1
     42 ;  (let ((x (random-in-range -1 1))
     43 ;        (y (random-in-range -1 1)))
     44 ;    (<= (+ (square x) (square y)) 1)))
     45 
     46 (define p
     47   (lambda ()
     48     (let ((x (random-in-range 0 2))
     49           (y (random-in-range 0 2)))
     50       (>= 1 (+ (square (- x 1)) (square (- y 1)))))))
     51 
     52 (define (random-in-range low high)
     53   (let ((range (- high low)))
     54     (+ low (random range))))
     55 
     56 (define square
     57   (lambda (x) (* x x)))
     58 
     59 (exact->inexact (estimate-integral p 0 2 0 2 1000))
     60 
     61 ;;;;;;;;3.6
     62 (define rand-init 1)
     63 
     64 (define rand-update
     65   (lambda (x)
     66     (+ x 1)))
     67 
     68 (define rand
     69   (let ((x rand-init))
     70     
     71     (define reset
     72       (lambda (value)
     73         (set!  x value)))
     74 
     75     (define generate
     76         (begin(set! x (rand-update x))
     77         x))
     78 
     79     (define (dispatch x)
     80       (cond ((eq? x 'generate) generate)
     81             ((eq? x 'reset) reset)
     82             (else (error "No such method -- rand" a))))
     83     dispatch))
     84 
     85 (define rand1 
     86    (let ((x rand-init)) 
     87      (define (dispatch message) 
     88        (cond ((eq? message 'generate) 
     89                (begin (set! x (rand-update x)) 
     90                       x)) 
     91              ((eq? message 'reset) 
     92                (lambda (new-value) (set! x new-value))))) 
     93      dispatch))
     94 
     95 (define rand2
     96   (let ((x rand-init))
     97     (define (generate)
     98       (set! x (rand-update x))
     99       x)
    100     (define (reset n)
    101       (set! x n))
    102     (define (dispatch a)
    103       (cond ((eq? 'generate a) (generate))
    104             ((eq? 'reset a) reset)
    105             (else (error "No such method -- rand" a))))
    106     dispatch))
    107 
    108 
    109 
    110 ;(rand 'generate)
    111 ;(rand 'generate)
    112 ;(rand 'generate)
    113 ;((rand 'reset) 3)
    114 ;(rand 'generate)
    115 
    116 (rand2 'generate)
    117 (rand2 'generate)
    118 (rand2 'generate)
    119 ((rand2 'reset) 3)
    120 (rand2 'generate)

    1.被注释掉的p 不是一个过程,而是一个值 如果把rand-in-range 放后面会报错

    2.也是同样的情况被注释掉的rand过程中 我试图直接把generate做成一个值,但是总是得不到正确的结果,

    我想可能是因为,当generate 为数值时,它的数值的取决于rand中的x,而在这里就会 出现 x一直为

    rand-init。如果把generate 做成一个无参过程,除了开始之外就不会依赖于rand 中的x了,从而每次都会update

    自己的x了,可能和作用域有关

    Yosoro
  • 相关阅读:
    绘图类
    画笔类
    创建模态,非模态对话框
    菜单类
    [WPF]程序随系统自启动
    [WPF]xml序列化以及反序列化数据
    [WPF]项目整合Metro和MaterialDesignInXamlToolkit UI框架
    [WPF]鼠标移动到Button颜色改变效果设置
    [Leetcode] 寻找数组的中心索引
    [Leetcode]在排序数组中查找元素的第一个和最后一个位置
  • 原文地址:https://www.cnblogs.com/tclan126/p/6531978.html
Copyright © 2011-2022 走看看