使用 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 

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

查看所有标签

猜你喜欢:

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

The Cult of the Amateur

The Cult of the Amateur

Andrew Keen / Crown Business / 2007-6-5 / USD 22.95

Amateur hour has arrived, and the audience is running the show In a hard-hitting and provocative polemic, Silicon Valley insider and pundit Andrew Keen exposes the grave consequences of today’s......一起来看看 《The Cult of the Amateur》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换