Skip to content

Instantly share code, notes, and snippets.

View yanfeng42's full-sized avatar
💌
I may be slow to respond.

Macro yanfeng42

💌
I may be slow to respond.
View GitHub Profile
@yanfeng42
yanfeng42 / sicp_1_22_chez.scm
Last active February 13, 2021 00:58
sicp 1.22 的chez scheme 实现.
;; 基于 https://cisco.github.io/ChezScheme/csug9.5/csug9_5.pdf 的 12.10. Times and Dates
(define (time-prime-test n)
(newline)
(display n)
(start-prime-test n (current-time))
)
(define (start-prime-test n start-time)
(if (prime? n)
@yanfeng42
yanfeng42 / sicp_1.28_expmode.scm
Created April 18, 2021 06:58
sicp_1.28 解法简单 问题晦涩
(define (miller-rabin-test n)
(define (try-it a)
; 因为 1 小于n, 所以 a/n 的余数就是 1.
(= (expmode a (- n 1) n) 1)
)
(try-it (+ 1 (random (- n 1))))
)
(define (expmod base exp m)
(define (squaremod-with-check x)
@yanfeng42
yanfeng42 / simple_pi.scm
Created April 18, 2021 12:03
sicp1.3 发现一个计算 Π 的方法
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b)
)
)
)
; 结果逼近 Π/8
@yanfeng42
yanfeng42 / simson-rule.scm
Last active April 22, 2021 02:07
sicp1.29 通用的高阶求和算法 + 辛普森积分算法
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b)
)
)
)
; 求立方.
@yanfeng42
yanfeng42 / john_wallis_pi.scm
Created April 25, 2021 02:47
sicp 1.31 另一个关于 pi 的计算公式 -- john wallis pi 的另一种形式理解
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b)
)
)
)
(define (product term a next b)
@yanfeng42
yanfeng42 / accumulate.scm
Created May 20, 2021 06:20
sicp 1.32定义通用高阶 累积 函数
(define (accumulate combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate combiner null-value term (next a) next b)
)
)
)
(define (accumulate combiner null-value term a next b)
@yanfeng42
yanfeng42 / filtered-accumulate.scm
Created May 24, 2021 06:23
sicp 1.33 带filter的accumulate.
(define (filtered-accumulate combiner null-value term a next b filter?)
(if (> a b)
null-value
(combiner (if (filter? a) (term a) null-value)
(filtered-accumulate combiner null-value term (next a) next b filter?)
)
)
)
(define (filtered-sum term a next b filter?)
@yanfeng42
yanfeng42 / sicp_1.39.scm
Created June 28, 2021 06:36
sicp 1.39 用单独符号, 还是用 系数 表达 "+" 和 "-"
; 我的答案:
; 递归写法.
(define (cont-frac-full n d k calculator)
(define (iterator i)
(if (< i k)
(/ (n i) (calculator (d i) (iterator (+ i 1))))
(/ (n i) (d i))
)
)
@yanfeng42
yanfeng42 / newtons-method.scm
Created July 7, 2021 05:56
sicp 1.40 计算任意 方程的近似解
(define (newton-transform g)
(lambda (x)
(- x (/ (g x) ((deriv g) x)))))
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)
)
)
@yanfeng42
yanfeng42 / png-size.scm
Created July 19, 2021 12:21
计算 png 图对应的 bitmap 图的大小
(define (png-size w h) (* w h 4.0 1/1024))