3

使用 lisp 简单描述加法运算

 1 year ago
source link: https://keelii.com/2016/07/04/describe-a-simple-add-method-use-lisp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用 lisp 简单描述加法运算

使用 lisp 简单描述加法运算

2016/7/4首页

最近在看 MIT 公开课-计算机程序的构造和解释,即使你像我一样根本没学过 lisp 也能看懂下面这段代码,这段代码展示了怎么实现加法运算,这种我们几乎从来不会去想为什么的问题,这几行简单的代码告诉我们如何计算出 3 + 4 的值

皮亚诺 算术定义的求 x 和 y 之和的过程

; Define a [+] processor
(define (+ x y)
    (if (= x 0)
        y
        (+ (-1+x) (1+y))))

; x = 3, y = 4
(+ 3 4)
(if (= 3 0) 4 (+ (-1+3) (1+4)))
(+ (-1+3) (1+4))
(+ 2 5)
(if (= 2 0) 5 (+ (-1+2) (1+5)))
(+ (-1+2) (1+5))
(+ 1 6)
(if (= 1 0) 6 (+ (-1+1) (1+6)))
(+ (-1+1) (1+6))
(+ 0 7)
(if (= 0 0) 7 (-1+0) (1+7))
7

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK