lisp

How do I increment or decrement a number in Common Lisp?

非 Y 不嫁゛ 提交于 2020-01-20 17:12:42
问题 What is the idiomatic Common Lisp way to increment/decrement numbers and/or numeric variables? 回答1: Use the built-in "+" or "-" functions, or their shorthand "1+" or "1-", if you just want to use the result, without modifying the original number (the argument). If you do want to modify the original place (containing a number), then use the built-in "incf" or "decf" functions. Using the addition operator: (setf num 41) (+ 1 num) ; returns 42, does not modify num (+ num 1) ; returns 42, does

Lisp和SICP

一笑奈何 提交于 2020-01-18 12:48:09
大概不少programmer都看过《黑客与画家》,作者用了整整一章的篇幅讨论Lisp的强大。我自然就会手痒痒。 几个月前,几天内攻城略地搞定了Python,用的方法便是用Py重写之前开发的类库,这样就能很快熟悉语法,培养语感。喜上眉梢的我,也尝试将同样的策略用在Lisp上,我开始查看它的语法和函数,比如如何定义类和函数,如何赋值等等。 但我慢慢发现,Lisp几乎都不需要学语法,就是括号和几个基本过程,无非就是lambda, define, let,cons,car,cdr等等。虽然语法很快就学完了,但却很难用它写出以前Python或C#风格的命令式代码。连赋值都变得如此诡异,而且有数不清的方言,定义类时,每种方言都不一样!函数名都超长,变量甚至会用问号感叹号等平时根本不敢想的符号。 在尝试写Lisp一天之后,我无奈的放弃了,它的思路和我之前所有的观念都很不相同! 后来我看了《计算机程序的构造与解释》(SICP). 这本书名气显然没有《编程珠玑》甚至《编程之美》名气大。但它的目录很对我的胃口,流模式,惰性求值,元语言抽象,听着是不是就很有逼格呢?于是我迫不及待的踏上这片土地,一边看一边做习题。 之后发生的事情超出了我的想象,这仅仅是一本MIT的大一新生的入门课程,其思维深度,习题难度和广度都超过了我的想象。怪不得国外有那么多大神,因为人家看过了这样牛逼的教材!它的函数求导和积分的方法

LISP之根源

被刻印的时光 ゝ 提交于 2020-01-18 12:47:47
约翰麦卡锡于1960年发表了一篇非凡的论文,他在这篇论文中对编程的贡献有如欧几里德对几何的贡献. 1 他向我们展示了,在只给定几个简单的操作符和一个表示函数的记号的基础上, 如何构造出一个完整的编程语言. 麦卡锡称这种语言为Lisp, 意为List Processing, 因为他的主要思想之一是用一种简单的数据结构表(list)来代表代码和数据. 值得注意的是,麦卡锡所作的发现,不仅是计算机史上划时代的大事, 而且是一种在我们这个时代编程越来越趋向的模式.我认为目前为止只有两种真正干净利落, 始终如一的编程模式:C语言模式和Lisp语言模式.此二者就象两座高地, 在它们中间是尤如沼泽的低地.随着计算机变得越来越强大,新开发的语言一直在坚定地趋向于Lisp模式. 二十年来,开发新编程语言的一个流行的秘决是,取C语言的计算模式,逐渐地往上加Lisp模式的特性,例如运行时类型和无用单元收集. 在这篇文章中我尽可能用最简单的术语来解释约翰麦卡锡所做的发现. 关键是我们不仅要学习某个人四十年前得出的有趣理论结果, 而且展示编程语言的发展方向. Lisp的不同寻常之处--也就是它优质的定义--是它能够自己来编写自己. 为了理解约翰麦卡锡所表述的这个特点,我们将追溯他的步伐,并将他的数学标记转换成能够运行的Common Lisp代码. 七个原始操作符 开始我们先定义 表达式 .表达式或是一个

【整理】LISP简介

浪子不回头ぞ 提交于 2020-01-18 12:47:36
张老师一直强调AutoCAD的开发有3种接口,vba,lisp,objectarx。objectarx功能强大,但学起来比较难。而vba和lisp就相对简单了。而且到时候用objectarx作出来的程序可以轻易调用lisp,所以只要做好lisp的话,也是可以最后汇总到我们的程序里面的。他希望项目组的其它同学能够使用Visual Lisp,参与到项目的开发中。于是我特地在网上百度了一下,收集整理了一些关于Lisp的信息。信息主要来源于百度百科的数个网页。 LISP的历史 LISP(全名LIST Processor,即链表处理语言),由约翰·麦卡锡在1960年左右创造的一种基于 λ演算 的函数式编程语言。 Lisp 代表 LISt Processing,即表处理,这种编程语言用来处理由括号(即“(”和“)”)构成的列表。约翰麦卡锡于1960年发表了一篇非凡的论文,他在这篇论文中对编程的贡献有如欧几里德对几何的贡献.[1] 他向我们展示了,在只给定几个简单的操作符和一个表示函数的记号的基础上, 如何构造出一个完整的编程语言. 麦卡锡称这种语言为Lisp, 意为List Processing, 因为他的主要思想之一是用一种简单的数据结构表(list)来代表代码和数据. 值得注意的是,麦卡锡所作的发现,不仅是计算机史上划时代的大事, 而且是一种在我们这个时代编程越来越趋向的模式

Recursing Through Nested List LISP

僤鯓⒐⒋嵵緔 提交于 2020-01-16 21:59:13
问题 How would I recurse through nested lists? For example, given: '((A 1 2) (B 3 4)) How would I add 2 to the second element in each nested sublist? (defun get-p0 (points) (loop for x from 0 to (- (list-length points) 1) do (+ 2 (cadr (nth x points))) ) ) I'm not really sure why (get-p0 '((A 1 2) (B 3 4))) returns NIL. 回答1: I'd go with something like this: (loop for (letter x y) in '((A 1 2) (B 3 4)) collect (list letter (+ 2 x) y)) The reason: it's shorter and you don't measure the length of the

Split a string even if the last character is a delimiter

…衆ロ難τιáo~ 提交于 2020-01-16 19:17:10
问题 I want to delete some characters at the end of a string. I made this function : (defun del-delimiter-at-end (string) (cond ((eq (delimiterp (char string (- (length string) 1))) nil) string ) (t (del-delimiterp-at-end (subseq string 0 (- (length string) 1))) ) ) ) with this : (defun delimiterp (c) (position c " ,.;!?/")) But I don't understand why it doesn't work. I have the following error : Index must be positive and not -1 Note that I want to split a string in list of strings, I already

max-lisp-eval-depth to find sqrt-iter

北慕城南 提交于 2020-01-16 11:12:10
问题 I am working on SICP's exercise 1.6 which rewrite the demonstration case #+begin_src emacs-lisp :session sicp :results output (defun sqrt(x) (sqrt-iter 1.0 x)) (defun sqrt-iter(guess x) (if (good-enough-p guess x) guess (sqrt-iter (improve guess x) x))) (defun good-enough-p(guess x) (< (abs (- (square guess) x)) 0.001)) (defun improve(guess x) (average guess (/ x guess))) (defun average(x y) (/ (+ x y) 2)) #+end_src It works and get the output #+begin_src emacs-lisp :session sicp :lexical t

How to update xdata information of an entity when it is copied

一笑奈何 提交于 2020-01-16 10:43:58
问题 I have two texts associated to a line. Because the texts represent some data of the line they are always considered children of the line and visible next to it. Through some lisp routines, if the data of the line change, the text entities reflect the change by changing their text. For that I have stored the handle of the line to each text as xdata and vice versa, e.g. the handles of the texts into the line. The problem arises when I copy the line with the texts where each one gets a new

How to update xdata information of an entity when it is copied

你说的曾经没有我的故事 提交于 2020-01-16 10:43:48
问题 I have two texts associated to a line. Because the texts represent some data of the line they are always considered children of the line and visible next to it. Through some lisp routines, if the data of the line change, the text entities reflect the change by changing their text. For that I have stored the handle of the line to each text as xdata and vice versa, e.g. the handles of the texts into the line. The problem arises when I copy the line with the texts where each one gets a new

How to update xdata information of an entity when it is copied

北城余情 提交于 2020-01-16 10:43:33
问题 I have two texts associated to a line. Because the texts represent some data of the line they are always considered children of the line and visible next to it. Through some lisp routines, if the data of the line change, the text entities reflect the change by changing their text. For that I have stored the handle of the line to each text as xdata and vice versa, e.g. the handles of the texts into the line. The problem arises when I copy the line with the texts where each one gets a new