使用 Lisp 简单描述加法运算

栏目: Lisp · 发布时间: 8年前

内容简介:使用 Lisp 简单描述加法运算

使用 Lisp 简单描述加法运算

最近在看 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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithm Analysis in Java

Data Structures and Algorithm Analysis in Java

Mark A. Weiss / Pearson / 2011-11-18 / GBP 129.99

Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course wa......一起来看看 《Data Structures and Algorithm Analysis in Java》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试