代码改变世界

随笔分类 -  fp_Lisp(scheme)

list append 总是复制前面的参数,而不复制最后一个参数

2014-02-16 16:22 by youxin, 803 阅读, 收藏, 编辑
摘要: append 总是复制前面的参数,而不复制最后一个参数(define a1 '(1 2 3))(define a2 '(a b c))(define x (append a1 a2))x; (1 2 3 a b c)(set-car! a2 'd)x; (1 2 3 d b c)(set-car! a1 4)x; (1 2 3 d b c)从这里可以看到,append 返回的 list 里,把第一个参数复制了一 份,而把第二个参数原封不动挂在了第一个参数的复制品后面。这个原因很简单,因为 append 如果不复制第一个参数,那么它就会 把第二个参数挂在真正的第一个参数后 阅读全文

Teach Yourself Scheme in Fixnum Days 13 Jump跳转

2014-02-16 15:07 by youxin, 485 阅读, 收藏, 编辑
摘要: JumpsOne of the signal features of Scheme is its support for jumps ornonlocal control. Specifically, Scheme allows program control to jump toarbitrarylocations in the program, in contrast to the more restrained forms of program control flow allowed by conditionals and procedure calls. Scheme’s nonlo 阅读全文

scheme 解释器Guile 使用

2014-02-13 14:53 by youxin, 2064 阅读, 收藏, 编辑
摘要: GNU Guile是一种Scheme编程语言的解释器和虚拟机。Guile是GNU Ubiquitous Intelligent Language for Extensions的缩写。Guile是GNU工程的官方扩展语言。Guile的理念是“开发者用C或C++实现关键算法和数据结构,并输出函数和类型供解释代码(interpreted code)使用。应用程序成为一组由解释器结合起来的原语,将编译代码的高效和解释代码的灵活结合起来。。Guile 问世于 1995 年,它是用于 Scheme 语言的解释器,Scheme 语言是简化 Lisp 语言得到的派生物,而 Lisp 语言则是由 John Mc 阅读全文

Teach Yourself Scheme in Fixnum Days 6 recursion递归

2014-02-13 13:26 by youxin, 559 阅读, 收藏, 编辑
摘要: A procedure body can contain calls to other procedures, not least itself:(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1))))))Thisrecursiveprocedure calculates thefactorialof a number. If the number is0, the answer is1. For any other numbern, the procedure uses itself t... 阅读全文

Common Lisp 编译器IDE环境搭建

2013-12-04 13:37 by youxin, 1793 阅读, 收藏, 编辑
摘要: 搭建Common Lisp编程环境的方法有很多种,这里我使用的是最常见的一种:SBCL + Emacs + SLIME。SBCL是SteelBankCommonLisp的简称,它是Common Lisp的一种高质量的开源实现。SBCL有windows和linux版本可供下载。安装完成后,进入环境后,应该是个星号:*(教程中是"CL-USER>")理论上就算环境ok了,不过简单验证下总没有坏处:*10(输入)10(展示结果)*(+ 1 2)(输入)3(展示结果)更多:http://iyuan.iteye.com/blog/1073458http://pchristens 阅读全文

Why Functional Programming Matters

2013-11-17 01:26 by youxin, 512 阅读, 收藏, 编辑
摘要: http://hi.baidu.com/lhurricane/item/35b57e12a1e3c5ddbf9042a7http://blog.csdn.net/ddwn/article/details/984390http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf 阅读全文

函数fold 或reduce用法

2013-11-17 01:23 by youxin, 2083 阅读, 收藏, 编辑
摘要: http://yi-programmer.com/2011-02-24_fold.htmlhttp://c2.com/cgi/wiki?FoldFunctionhttp://rwh.readthedocs.org/en/latest/chp/4.htmlThePythonLanguagecalls itreduce; this is a left fold: reduce(operator.add, [1,2,3,4]) reduce(lambda x,y: x+y, [1,2,3,4])You can get the effect of the parameter callediniti.. 阅读全文

Lisp之根源

2013-11-17 01:05 by youxin, 370 阅读, 收藏, 编辑
摘要: 原文:http://www.paulgraham.com/rootsoflisp.html约翰麦卡锡于1960年发表了一篇非凡的论文,他在这篇论文中对编程的贡献有如 欧几里德对几何的贡献.1他向我们展示了,在只给定几个简单的操作符和一个 表示函数的记号的基础上, 如何构造出一个完整的编程语言. 麦卡锡称这种语 言为Lisp, 意为List Processing, 因为他的主要思想之一是用一种简单的数据 结构表(list)来代表代码和数据.值得注意的是,麦卡锡所作的发现,不仅是计算机史上划时代的大事, 而且是一种 在我们这个时代编程越来越趋向的模式.我认为目前为止只有两种真正干净利落, 始终如一 阅读全文

scheme Continuation

2013-11-17 00:19 by youxin, 1448 阅读, 收藏, 编辑
摘要: Continuation Pass Style在函数式编程(FP)中有一种被称为Continuation Passing Style(CPS)的风格。在这种风格的背后所蕴含的思想就是将处理中可变的一部分抽象为一个function,并将其作为一个参数传入。这是高度抽象的方法,所带来的表达的威力也是无与伦比的。下面举一个例子,实现CPS描述的fold函数。在此之前需要简单介绍一下fold概念,在FP中这是一个非常有效的处理list的工具。一般它的signature是fold(f,initial,list)。它所对应的效果是:f(e0,f(e1,….f(en,initial)…),e0到en也就是l 阅读全文

scheme 宏macro写法

2013-11-16 23:20 by youxin, 4603 阅读, 收藏, 编辑
摘要: scheme里的宏不同的实现有不同的写法:1.mzscheme的define-macro (mzscheme也就是pltschme,也就是drracket,没有define-macro这个关键字)语法:(define-macro macro-name (lambda macro-args) macro-body ......)例如:定义when(define-macro when (lambda (test . branch) `(if ,test (begin ,@brach))))其中“·”重音号引入模版,逗号开始的符号为参数,逗号和@开始的被... 阅读全文

scheme 模拟queue

2013-11-16 22:03 by youxin, 313 阅读, 收藏, 编辑
摘要: [code 1] shows a implementation of queue. The functionenqueue!returns a queue in that theobjis added at the last of thequeue. The functiondequeue!removes the first item from the queue and return the first item.[code 1](define (make-queue) (cons '() '()))(define (enqueue! queue obj) (let ((lo 阅读全文

scheme I/0 输入输出操作

2013-11-16 19:33 by youxin, 1914 阅读, 收藏, 编辑
摘要: 2.1.open-input-file,read-char, andeof-object?The function (open-input-filefilename) is available to open a file. This function return a port for input. The function (read-charport) is to read a character from theport. As this function returnseof-objectwhen it reaches the end of the file (EOF), you c 阅读全文

scheme递归

2013-11-16 17:25 by youxin, 536 阅读, 收藏, 编辑
摘要: 主要参考:http://www.shido.info/lisp/scheme7_e.htmlFunctionfactthat calculates factorials.(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))(fact 5) is calculated like as follows:(fact 5)⇒ 5 * (fact 4)⇒ 5 * 4 * (fact 3)⇒ 5 * 4 * 3 * (fact 2)⇒ 5 * 4 * 3 * 2 * (fact 1)⇒ 5 * 4 * 3 * 2 * 1⇒ 5 *... 阅读全文

MIT-scheme安装

2013-11-16 16:49 by youxin, 1770 阅读, 收藏, 编辑
摘要: 下载地址:http://www.gnu.org/software/mit-scheme/下载windows版本,安装。The MIT-Scheme can be installed by just downloading and executing the installer.Go tothe homepage of MIT/GNU Schemeand download the Windows binary,mit-scheme-N.N.N-ix86-win32.exe.Double click the downloaded installer. The installer asks some 阅读全文

scheme一页纸教程

2013-11-16 16:36 by youxin, 620 阅读, 收藏, 编辑
摘要: 这是一个大学教授写的,非常好,原文:http://classes.soe.ucsc.edu/cmps112/Spring03/languages/scheme/SchemeTutorialA.htmlIntroductionStructureSyntaxTypesSimpleCompositeType PredictesNumbers, Arithmetic Operators, and FunctionsArithmetic OperatorsListsBoolean ExpressionsLogical OperatorsRelational OperatorsConditional Ex 阅读全文

《how to design programs》12章函数复合

2013-11-13 19:19 by youxin, 476 阅读, 收藏, 编辑
摘要: 我们写代码时要学会适应辅助函数。作者提出了一个问题,如何对一个表排序。排序函数读取一个表,产生另一个表。排序函数的合约和用途如下:(sort empty);; expected value: empty(sort (cons 1297.04 (cons 20000.00 (cons -505.25 empty))));; expected value: (cons 20000.00 (cons 1297.04 (cons -505.25 empty)))完整程序:(实际上就是插入排序)#lang racket;12;;sort:list-of-numbers->list-of-numbe 阅读全文

lisp构造表

2013-11-12 19:31 by youxin, 925 阅读, 收藏, 编辑
摘要: CONS 操作符我们刚刚学习了如何拆分一个表,现在学习如何合并一个表。 CONS 操作符就是做这件事情的。假设有一个列表 (1 2 3) ,我们做一下 CAR 操作:(car '(1 2 3))返回 1 。我们再做一下 CDR 操作:(cdr '(1 2 3))返回 (2 3) 。CONS 操作符的作用就是将拆开的表连起来。(cons 1 '(2 3))返回的将是原来的列表 (1 2 3) 。S 表达式cons 操作符的第二个参数要是一个列表,才能返回一个列表。否则:(cons 2 3)返回(2 . 3)这次中间有一个点。为什么呢?因为,表实际上是一个树(二叉树)。我们 阅读全文

《how to design programs》第11章自然数

2013-11-12 19:12 by youxin, 356 阅读, 收藏, 编辑
摘要: 这章让我明白了原来自然数的定义本来就是个递归的过程。我们通常用枚举的方式引出自然数的定义:0,1,2,3,等等(etc).最后的等等是什么意思?唯一能把等等从描述自然数的枚举方法中去除的方法是自引用,第一种尝试是:0 is a natural number.Ifnis a natural number, then one more thannis one, too.虽然这种描述并不是十分严格,但对于scheme格式的数据定义来说,他是一个很好的开始:natural-number(自然数是以下2者之一1。02.(add1 n) 如果n个自然数0是第一个自然数(add1 0)是下一个,接着就明显了 阅读全文

《how to design programs》第10章表的进一步处理

2013-11-12 16:13 by youxin, 281 阅读, 收藏, 编辑
摘要: 返回表的函数:下面是一个求工资的函数:;; wage : number -> number;; to compute the total wage (at $12 per hour);; of someone who worked for h hours(define (wage h) (* 12 h))显示,只能求单个人的工资,我们相求多个人的工资,于是想传入一个list过去:;; hours->wages : list-of-numbers -> list-of-numbers;; to create a list of weekly wages from a list 阅读全文

转:SCHEME 语言是怎么来的 -1

2013-11-12 11:31 by youxin, 608 阅读, 收藏, 编辑
摘要: 导言Scheme 是 LISP 的一个方言(dialect)。著名的 SICP 书就是以 Scheme 为教学语言(实际上 SICP 的作者就是 Scheme 的作者)。 虽然 Scheme 本身只是一个精简化的适合教学的语言,可它首先提出的一些重要的思想,引领了新一代的LISP语言的出现。 实际上, LISP 语言发展的历史是连续的,之所以我在这里人为的把 LISP 的发展史划分为上一代和现代,是因为随着 Scheme 首次引入并规范化了一些重要概念, LISP 语言出现了很多以前从来没有大规模普及的新特性。以 Common LISP 为代表的 LISP 语言也因为这些新特性,而焕发了第二春 阅读全文
点击右上角即可分享
微信分享提示