Skip to content

Instantly share code, notes, and snippets.

Avatar
💌
I may be slow to respond.

颜风 ios122

💌
I may be slow to respond.
View GitHub Profile
@ios122
ios122 / sicp_1_22_chez.scm
Last active Feb 13, 2021
sicp 1.22 的chez scheme 实现.
View sicp_1_22_chez.scm
;; 基于 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)
@ios122
ios122 / expmode-better.scm
Last active Jan 24, 2021
sicp 1.2.6 费马小定理 核心代码的 尾递归优化版实现 与 实现正确性的证明(with todo)
View expmode-better.scm
; 书上的初版实现
(define (expmode base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmode base (/ exp 2) m)) m))
(else
(remainder (* base (expmode base (- exp 1) m)) m))
)
)
@ios122
ios122 / fermat-test.scm
Last active Jan 21, 2021
sicp 1.2.6, 关于费马小定理实现中的, 若a^n % b = x, 则: a^2n % b = x^2 % b 的简易证明
View fermat-test.scm
;费马小定理: 如果 n 是一个素数, a是小于n 的任意正整数, 那么 a的n次方 与 a 模n同余(除以 n 的余数相同).
; 计算一个数的幂, 对另一个数取模的结果.
(define (fermat-test n)
(define (try-it a)
; 因为 a 小于n, 所以 a/n 的余数就是 a.
(= (expmode a n n) a)
)
(try-it (+ 1 (random (- n 1))))
)
@ios122
ios122 / application_order_or_regular_order.scm
Created Jan 8, 2021
sicp 1.5 检测 解释器 是 正则序还是应用序求值
View application_order_or_regular_order.scm
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y)
)
; 应用序求值(先求值参数而后应用), 会陷入死循环; 正则序求值(完全展开而后归约), 会输出0.
(test 0 (p))
@ios122
ios122 / my-gcd.scm
Created Jan 8, 2021
sicp 1.2.5 最大公约数(gcd) 的欧几里得算法
View my-gcd.scm
(define (my-gcd a b)
(if (= b 0)
a
(gcd b (remainder a b)))
)
@ios122
ios122 / fix_grpc_int64_convert_errr.swift
Created Jan 7, 2021
Swift 基于 Codable 来解决 grpc 自动将 int64 转为 String 带来的不便.
View fix_grpc_int64_convert_errr.swift
final class SampleReply: Codable {
let code: Int32 // 状态码
private let _pageToken: String
lazy var pageToken: Int64 = { return Int64(self._pageToken) ?? 0 }()
private enum CodingKeys: String, CodingKey {
case code
case _pageToken = "pageToken"
}
}
@ios122
ios122 / sicp_1.17.scm
Created Jan 3, 2021
sicp 1.17 尾递归解法的证明
View sicp_1.17.scm
; ref: http://community.schemewiki.org/?sicp-ex-1.17
(define (fast-mult-by-add a b)
(define (double x) (+ x x))
(define (halve x) (/ x 2))
(define (helper a b product) ;; "add a" b times
(cond ((= b 0) product)
((even? b) (helper (double a) (halve b) product))
(else (helper a (- b 1) (+ a product)))))
(helper a b 0))
@ios122
ios122 / logB.scm
Created Dec 31, 2020
可能很有用的工具方法: 计算任意底数的对数
View logB.scm
(define logB
(lambda (x B)
(/ (log x) (log B))))
(define (log3 x)
(logB x 3)
)
; (log3 9) -> 2.0
@ios122
ios122 / 1.14_1.scm
Created Dec 30, 2020
sicp 练习1.14 (2) 计算步数增长的阶(时间复杂度)
View 1.14_1.scm
(define (count-change amount)
(cc amount 5 0)
)
(define (cc amount kinds-of-coins depth)
(define from-node (cc-node-depth amount kinds-of-coins depth))
(display (cc-node-depth-label amount kinds-of-coins depth))
(cond
((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
@ios122
ios122 / count-change-gv.scm
Created Dec 28, 2020
SICP练习1.14. 使用 GrapViz 可视化 零钱问题的 递归计算步骤.
View count-change-gv.scm
(define (count-change amount)
(cc amount 5 0)
)
(define (cc amount kinds-of-coins depth)
(define from-node (cc-node-depth amount kinds-of-coins depth))
(display (cc-node-depth-label amount kinds-of-coins depth))
(cond
((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)