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 / midpointSegment.swift
Created August 11, 2021 04:13
2.2 的 swift 版实现 -- 先用swift 模拟 cons car cdr
// mirror...
func cons<T0, T1>(_ x: T0, _ y: T1) -> (T0, T1) {
return (x, y)
}
func car<T0, T1>(_ t:(T0, T1)) -> T0 {
return t.0
}
func cdr<T0, T1>(_ t:(T0, T1)) -> T1 {
@yanfeng42
yanfeng42 / iterative-improve.scm
Last active August 3, 2021 02:04
1.46 迭代式改进的一般 形式.
; 迭代式改进. -- 哈哈哈. 最终的社区精简版, 竟然是我这种写法. --- 编程理念的差异. 在可读性和技巧性的取舍上, 我优先选择 可读性.
(define (iterative-improve good-enough? impove)
(define (f x)
(if (good-enough? x)
x
(f (impove x))
)
)
f
)
@yanfeng42
yanfeng42 / png-size.scm
Created July 19, 2021 12:21
计算 png 图对应的 bitmap 图的大小
(define (png-size w h) (* w h 4.0 1/1024))
@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 / 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 / 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 / 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 / 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 / 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 / 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