lisp

Common Lisp 高阶函数学习笔记: function, funcall 和 apply...

孤街醉人 提交于 2019-12-07 21:49:58
Common Lisp 高阶函数学习笔记: function, funcall 和 apply 的用法 目录 0 概述 1 函数 function 的用法 2 函数 funcall 的用法 3 函数 apply 的用法 0 概述 高阶函数是 Lisp 的一大特色, 所谓的高阶函数就是把一个函数当做另一个函数的参数来用, 如果把普通的函数调用看做是在二维平面上的活动, 那么高阶函数就相当于增加了一个维度:可以把高阶函数看做在三维立体空间的活动. 一般来说, 编程语言需要这种机制, 因为这样可以为开发者提供更灵活更高级的结构抽象能力, 正如<实用 Common Lisp 编程>中所说: 在 C 语言中使用函数指针, Perl 使用子例程引用, Python 跟 Lisp 一样, C# 使用代理, Java 则使用反射和匿名类. 高阶函数的一个应用场景是通用排序, 比如 Lisp 的函数 sort , 调用形式如下: (sort '(6 2 5 3 7 0) #'>) 我们可以选择不同的比较函数(就是 #' 后面的 >), 这样的实现就比较灵活, 当我们想换一个比较函数, 如换成 < , 我们并不需要修改 sort 的代码, 只要新写一个 < 函数, 然后把它作为 sort 的参数传递进去就可以了. 另外回调函数和钩子也能够保存代码引用以便于以后运行. 1 函数 function 的用法

Error setting load-noisily? and auto-exiting in MIT-Scheme

帅比萌擦擦* 提交于 2019-12-07 20:28:38
问题 In order to debug MIT-Scheme scripts with Vim, I want to be able to run the script file currently being edited as conveniently as possible. Here is what I'm doing: sicp.scm (set! load-noisily? #t) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)) ) ) (abs 42) (abs -24) (exit) After executing :!mit-scheme --eval "(load \"sicp\")" when editing sicp.scm in Vim, I get: Image saved on Saturday May 17, 2014 at 2:39:25 AM Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR

Y分钟学Common Lisp

£可爱£侵袭症+ 提交于 2019-12-07 19:09:41
ANSI Common Lisp 是一个广泛通用于各个工业领域的、支持多种范式的编程语言。 这门语言也经常被引用作“可编程的编程语言”(可以写代码的代码)。 免费的经典的入门书籍 《实用 Common Lisp 编程》 另外还有一本热门的近期出版的 Land of Lisp . 语法 一般形式 Lisp有两个基本的语法单元:原子(atom),以及S-表达式。一般的,一组S-表达式被称为“组合式”。 10 ; 一个原子; 它对自身进行求值 :THING ;同样是一个原子;它被求值为一个符号 :thing t ;还是一个原子,代表逻辑真值。 (+ 1 2 3 4) ; 一个S-表达式。 '(4 :foo t) ;同样是一个S-表达式。 注释 一个分号开头的注释表示仅用于此行(单行);两个分号开头的则表示一个所谓标准注释;三个分号开头的意味着段落注释;而四个分号开头的注释用于文件头注释(译者注:即对该文件的说明)。 #| 块注释 可以涵盖多行,而且... #| 他们可以被嵌套! |# |# 运行环境 有很多不同的Common Lisp的实现;并且大部分的实现是一致(可移植)的。对于入门学习来说,CLISP是个不错的选择。 可以通过QuickLisp.org的Quicklisp系统管理你的库。 通常,使用文本编辑器和“REPL”来开发Common Lisp;(译者注:“REPL”指读取

How to translate the following into a tail recursive procedure?

…衆ロ難τιáo~ 提交于 2019-12-07 18:35:31
问题 I have the following mathematical expression: ; f(n) = f(n - 1) + f(n - 2) where n >= 2 ; f(n) = n where n < 2` Which I translated into a normal recursive LISP call: (define (f n) (cond ((< n 2) n) (else (+ (f (- n 1)) (f (- n 2)))))) How would I translate the above into a tail-recursive procedure? I'm not used to functional programming so I'm struggling a bit. 回答1: You are talking about established example of tail recursive transformation for computing Fibonacci numbers. You can find

LISP very simple list question

风格不统一 提交于 2019-12-07 17:11:00
问题 Im learning lisp and im pretty new at this so i was wondering... if i do this: (defparameter *list-1* (list 1 2)) (defparameter *list-2* (list 2 3)) (defparameter *list-3* (append *list-1* *list-2*)) And then (setf (first *list-2*) 1) *list-3* I will get (1 2 1 4) I know this is because the append is going to "save resources" and create a new list for the first chunk, but will actually just point to the second chunk, coz if i do: (setf (first *list-1*) 0) *list-3* I will get (1 2 1 4) instade

Passing a list of functions as an argument in common Lisp

断了今生、忘了曾经 提交于 2019-12-07 15:20:25
Say there is a function F. I want to pass a list of functions as an argument into function F. Function F would go through each function in the list one by one and apply each one to two integers: x and y respectively. For example, if the list = (plus, minus, plus, divide, times, plus) and x = 6 and y = 2 , the output would look like this: 8 4 8 3 12 8 How do I implement this in common Lisp? There are many possibilities. CL-USER> (defun f (x y functions) (mapcar (lambda (function) (funcall function x y)) functions)) F CL-USER> (f 6 2 (list #'+ #'- #'+ #'/ #'* #'+)) (8 4 8 3 12 8) CL-USER> (defun

How does Lisp function remember state in this code?

﹥>﹥吖頭↗ 提交于 2019-12-07 14:29:43
问题 I saw a piece of code from the website http://www.ccs.neu.edu/home/shivers/newstyle.html: > (defun element-generator () (let ((state '(() . (list of elements to be generated)))) ;() sentinel. (let ((ans (cadr state))) ;pick off the first element (rplacd state (cddr state)) ;smash the cons ans))) ELEMENT-GENERATOR > (element-generator) LIST > (element-generator) OF > (element-generator) ELEMENTS > (element-generator) TO > (element-generator) BE > (element-generator) GENERATED I don't

Common Lisp String 常用函数用法

馋奶兔 提交于 2019-12-07 13:08:21
Strings (char string i) Function Returns the ith character of string. Zero-indexed. Ignores fill pointers. Settable. > (char "Floob-Boober" 0) => #\F > (char "Floob-Boober" 1) => #\l (char s j) == (aref (the string s) j) (make-string n &key initial-element {element-type 'character)) Function Returns a new string of n initial-elements (the default values of which is implementation-dependent). > (make-string 4) => "^@^@^@^@" ; "^@" == \0 > (make-string 4 :initial-element #\L) => "LLLL" (schar simple-string i) Function Like char but the string must be a simple string. Settable. same as (char

Cocoa 用户界面组件使用指导(教程翻译)

蓝咒 提交于 2019-12-07 12:55:11
Cocoa 用户界面组件使用指导(教程翻译) === 原文地址: 本地: ~/ccl-1.8-darwinx86/examples/cocoa/ui-elements/HOWTO.html 网络: http://trac.clozure.com/ccl/browser/trunk/source/examples/cocoa/ui-elements/HOWTO.html 原文标题: UI Elements HOWTO 翻译者: FreeBlues 2013-07-18 === 目录 0 概述 1 创建一个窗口 2 增加一个按钮 3 增加按钮相关的事件处理函数 0 概述 这篇 HOWTO 文档示范了如何通过 Lisp 调用来实例化和初始化 Objective-C 对象,进而创建 Cocoa 用户界面组件。 Cocoa 程序员通常使用苹果的 InterfaceBuilder 应用程序来创建的 UI 组件,然后从一个 nibfile 文件中加载这些组件,但是 Cocoa 支持在 Objective-C 方法调用中创建所有的相同的 UI 组件。事实上,调用 nibfiles 就是这是怎么一回事:通过方法调用来实例化那些在 nibflie 中描述的对象。 Lisp 程序员习惯于在工作中采取增量和交互的方式,所以通过方法调用来交互式创建用户界面组件,比起在 InterfaceBuilder

Clozure Common Lisp 接口数据库创建操作指导(教程翻译)

僤鯓⒐⒋嵵緔 提交于 2019-12-07 12:53:09
Clozure Common Lisp 接口数据库创建操作指导(教程翻译) === 原文地址: 网络: http://trac.clozure.com/ccl/browser/trunk/source/examples/cocoa/interface-databases/ 本地: file:///ccl-1.8-darwinx86/examples/cocoa/interface-databases/HOWTO.html 原文标题: Interface Databases HOWTO 翻译者: FreeBlues 2013-07-21 特别说明: 有了这个指导应该可以按图索骥地亲自创建那些自己需要框架的接口数据库了,不过我还没亲自试验过,改天亲自试验之后再更新这篇翻译. 目录 0 关于接口数据库 About Interface Databases 0.1 在线文档 Online Documentation 0.2 接口数据库和外部函数接口 Interface Databases and the Foreign Function Interface 0.3 接口数据库和框架 Interface Databases and Frameworks 1 增加新的接口数据库Adding New Interface Databases 1.1 获取和安装ffigen Obtain and