作者:
Conrad Barski M.D. 出版社: No Starch Press 副标题: Learn to Program in Lisp, One Game at a Time! 出版年: October 2009 (est.) 页数: 504 定价: 386.00 元 装帧: Perfect Paperback ISBN: 9781593272005
Lisp is a uniquely powerful programming language that, despite its academic reputation, is actually very practical. Land of Lisp brings the language into the real world, teaching Lisp by showing readers how to write several complete Lisp-based games, including a text adventure, an evolution simulation, and a robot battle. While building these games, readers learn the core conce...
Lisp is a uniquely powerful programming language that, despite its academic reputation, is actually very practical. Land of Lisp brings the language into the real world, teaching Lisp by showing readers how to write several complete Lisp-based games, including a text adventure, an evolution simulation, and a robot battle. While building these games, readers learn the core concepts of Lisp programming, such as data types, recursion, input/output, object-oriented programming, and macros. And thanks to the power of Lisp, the code is short. Rather than bogging things down with reference information that is easily found online, Land of Lisp focuses on using Lisp for real programming. The book is filled with the author Conrad Barski's famous Lisp cartoons, featuring the Lisp alien and other zany characters.
The ability to pass around functions as if they were just plain old pieces of
data is incredibly valuable. Once you get used to doing this, you open up all
kinds of conceptual possibilities in the design of your programs. Eventually,
your programs will start looking very different from programs in more (dare
I say) pedestrian languages, such as Java or C. The name for the style of pro-
gramming that relies heavily on passing functions as values is called higher-order
functional programming. We will look at this style in more detail in Chapter 14.
An even more important reason why Lispers go gaga over lambda is that,
as it turns out, in a purely mathematical sense, lambda is actually the only Lisp
command there is!
Recall that Lisp is unusual among programming languages in that it... (查看原文)
Common Lisp的入门书籍我共读过3本。 第一次,我开始读PracticalCommonLisp.chm,但是书籍给我一种不够流畅和有意思的感觉。读到第3章中断。 https://www.dropbox.com/s/2y26d4pe3kjnhvt/PracticalCommonLisp.chm?dl=0 第二次,慕名而来,开始读ANSI Common LISP,但是书籍...
(展开)
(defparameter *nodes* '((living-room (you are in the living-room.
a wizard is snoring loudly on the couch.))
(garden (you are in a beautiful garden.
there is a well in front of you.))
(attic (you are in the attic.
there is a giant welding torch in the corner.))))
查找地点使用 assoc 函数,获得关联表中 key 对应的值:
? (assoc 'garden *nodes*)
(GARDEN (YOU ARE IN A BEAUTIFUL GARDEN. THERE IS A WELL IN FRONT OF YOU.))
(defun describe-location (location nodes)
(cadr (assoc location nodes)))
(defparameter *edges* '((living-room (garden west door)
(attic upstairs ladder))
(garden (living-room east door))
(attic (living-room downstairs ladder))))
(defun describe-path (edge)
`(there is a ,(caddr edge) going ,(cadr edge) from here.))
? (describe-path '(garden west door))
(THERE IS A DOOR GOING WEST FROM HERE.)
(defun describe-paths (location edges)
(apply #'append (mapcar #'describe-path (cdr (assoc location edges)))))
? (describe-paths 'living-room *edges*)
(THERE IS A DOOR GOING WEST FROM HERE. THERE IS A LADDER GOING UPSTAIRS FROM HERE.)
#' 符号等价于 function 函数, 即 #'append == (function append)
因为 Common Lisp 具有两个 namespace: 变量与函数,在两个空间中可以分别具有相同名字的两个不同值。如下:
在let的局部block中, car 表示一个变量值为1 , 而 #'car 则表示内置函数。
(defun describe-objects (loc objs obj-loc)
(labels ((describe-obj (obj)
`(you see a ,obj on the floor.)))
(apply #'append (mapcar #'describe-obj (objects-at loc objs obj-loc)))))
? (describe-objects 'living-room *objects* *object-locations*)
(YOU SEE A WHISKEY ON THE FLOOR. YOU SEE A BUCKET ON THE FLOOR.)
? (look)
(YOU ARE IN A BEAUTIFUL GARDEN. THERE IS A WELL IN FRONT OF YOU. THERE IS A DOOR GOING EAST FROM HERE. YOU SEE A FROG ON THE FLOOR. YOU SEE A CHAIN ON THE FLOOR.)
? (pickup 'chain)
(YOU ARE NOW CARRYING THE CHAIN)
? (look)
(YOU ARE IN A BEAUTIFUL GARDEN. THERE IS A WELL IN FRONT OF YOU. THERE IS A DOOR GOING EAST FROM HERE. YOU SEE A FROG ON THE FLOOR.)
Lisp feature: 1. function result is returned automaticall by called function.( retur... is not necessary in lisp) 2.Symbols in Common Lisp are case-insensitive Clisp内置函数列表:ash (num offest) 把ash向左移动offset位。offset为正数时向左,为负数时向右移动。 (1- variable) 把变量的值-1 (1+ variable) return variable + 1 ; (/ var1 var2) (oddp var) ...
2011-05-29 23:30:591人喜欢
Lisp feature:
1. function result is returned automaticall by called function.( retur... is not necessary in lisp)
2.Symbols in Common Lisp are case-insensitive引自第21页
(eval var) 执行var里的data。
Lisp conception
1.form:对lisp中code列表的称呼。即一个list中的所有元素都将被视作code,compiler将会执行所有的元素。 如(expt 2 4)
2.quoting:对lisp中data列表的称呼。即一个list中的所有元素都会别看做data,compiler不会执行他们。 要把一个list作为data list 即在list的最前面加上一个单引号。顾名思义叫做quoting。 如‘(expt 2 4)
3.cons cells:指LIsp中的所有括号包围着的符号,数字,字符串。他的功能就是把所有的Lisp元素结合在一起,形成一个有机整体。逻辑上指的是数据结构中list的每一个节点。 对于cons 和 cells 维基百科上有更详细的解释。http://en.wikipedia.org/wiki/Cons
4homoiconic:A programming language that uses the same data structures to store data and program code is called homoiconic.维基百科上的解释http://en.wikipedia.org/wiki/Quasiquote
Lisp
defining a global variable
(defparmeter *variable* init-value)
defining a local varibale
(let (variable declarations) ..body... )
e.g (let ((a 1) (b 4))
(+ a b) )
declaring a global variable
(defvar *variable* init-value)
defining a global function
(defun function-name (arguments) ...)
defining a local function
(flet ((function-name (arguments)
....function body........))
....block body..........)
e.g (flet ((fun (n) (+ n 10)))
(fun 7) )
to make function names available in other defined function(can be called itself)
(labels ((a (n)
(+ a 5))
(b (n)
(+ (a n) 6)) )
声明:大家如果发现上文有不对的地方,还请大家原谅。毕竟本人学识浅薄。英文又不行。欢迎大家批评指正哈。
第三章 探索Lisp代码的语法 要弄懂为什么Lisp的语法那么怪异,需要从语法和语义说起。Lisp的语法非常简单,这是它区别于其他语言很重要的特点。 相比较来说,C++的语法要远比Lisp复杂多了。 《The Root of Lisp》一文中总结的,基本Lisp只有7个语法结构。 要写一个C++的编译器,是极端困难的,而写一个Lisp的编译器或解释器就要简单很多。那是,Lisp的代码本身就是准备好的语法树了。这也难怪会有成百上千的各类Lisp实现了。 ...(2回应)
2011-02-15 23:52:082人喜欢
第三章 探索Lisp代码的语法
要弄懂为什么Lisp的语法那么怪异,需要从语法和语义说起。Lisp的语法非常简单,这是它区别于其他语言很重要的特点。
相比较来说,C++的语法要远比Lisp复杂多了。
《The Root of Lisp》一文中总结的,基本Lisp只有7个语法结构。
要写一个C++的编译器,是极端困难的,而写一个Lisp的编译器或解释器就要简单很多。那是,Lisp的代码本身就是准备好的语法树了。这也难怪会有成百上千的各类Lisp实现了。
例如: http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours
Write scheme in 48 hours (Haskell写scheme解释器).
Lisp将代码组织成嵌套的列表。
Lisp代码本身就是语法树这一点,也是Lisp强大的地方,通过Macro宏,可以在任何时候来修改这个语法树,也就是可以写出“能写程序的程序”。Lisp强大的根源就在于它那一堆括号的写法。
列表中可以放入各类数据, 如 symbol, numbers, strings
Numbers , 可以计算大数, 也可以用有理数表示,对于科学计算比较方便
copy过来变得好丑。。。 (defparameter *nodes* '((living-room (you are in the living-room. a wizard is snoring loudly on the couch.)) (garden (you are in a beautiful garden. there is a well in front of you.)) (attic (you are in the attic. there is a giant welding torch in the corner.)))) (defun describe-location (location nodes) (cadr (assoc location nodes))) (defparameter *edges* '((living-room ...
2013-07-10 01:29:01
copy过来变得好丑。。。
(defparameter *nodes* '((living-room
(you are in the living-room.
a wizard is snoring loudly on the couch.))
(garden (you are in a beautiful garden. there is a well in front of you.))
(attic (you are in the attic. there is a giant welding torch in the corner.))))
(defun describe-location (location nodes)
(cadr (assoc location nodes)))
(defparameter *edges* '((living-room
(garden west door)
(attic upstairs ladder))
(garden
(living-room east door))
(attic
(living-room downstairs ladder))))
(defun describe-path (edge)
`(there is a , (caddr edge) going , (cadr edge) from here.))
(defun describe-paths (location edges)
(apply #' append (mapcar
#' describe-path (cdr (assoc location edges)))))
(defparameter *objects* '(whiskey bucket frog chain))
(defparameter *object-locations* '((whiskey living-room)
(bucket living-room)
(chain garden)
(frog garden)))
(defun objects-at (loc objs obj-locs)
(labels (at-loc-p (obj))
(eq (cadr (assoc obj obj-locs)))
(remove-if-not #' at-loc-pobjs)))
(defun describe-objects (loc objs obj-loc)
(labels ((describe-obj (obj)
`(you see a , obj on the floor.)))
(apply #' append (mapcar #' describe-obj (objects-at loc objs obj-loc)))))
Empty equals false. > (defun my-length (test-list) (if test-list (1+ (my-length (cdr test-list))) 0)) > (my-length '(a b c d)) 4 '() eq () eq 'nil eq nil conditional commands in Lisp are typically special forms, which give it special privileges, such as the right to not evaluate all its parameters. It's impossible to do two or more separate things inside one if.(functional programmi...
2013-07-07 02:37:33
Empty equals false.
> (defun my-length (test-list)
(if test-list
(1+ (my-length (cdr test-list)))
0))
> (my-length '(a b c d))
4
'() eq () eq 'nil eq nil
conditional commands in Lisp are typically special forms, which give it special privileges, such as the right to not evaluate all its parameters.
It's impossible to do two or more separate things inside one if.(functional programming)
progn
with progn, only the last evaluation is returned as the value of the full expression.
when
with when, all the enclosed expressions are evaluated when the condition is true.
unless
with unless, all the enclosed expressions are evaluated when the condition is false.
cond
with cond, dose it all
and and or
Boolean evaluation
once lisp determines an earlier statement in a list of or values is true, t simply return true and doesn't bother evaluation the remaining statements.
case-insensitive two modes: code mode & data mode default: code mode '(单引号): data mode(quoting) cons cells There basis Functions for manipulating cons cells: cons, car, and cdr. cons: > (cons 'a 'b) (A . B) > (cons 'a 'nil) (A) > (cons 'a '(b c)) (A B C) > (cons '(a b) '(c d)) ((A B) C D) car: first slot; cdr: second slot > (car '(a b c)) A > (cdr '(a b c)) (B C) &...
2013-07-07 01:40:25
case-insensitive
two modes: code mode & data mode
default: code mode
'(单引号): data mode(quoting)
cons cells
There basis Functions for manipulating cons cells: cons, car, and cdr.
cons:
> (cons 'a 'b)
(A . B)
> (cons 'a 'nil)
(A)
> (cons 'a '(b c))
(A B C)
> (cons '(a b) '(c d))
((A B) C D)
car: first slot; cdr: second slot
> (car '(a b c))
A
> (cdr '(a b c))
(B C)
>(cadr '(a b c))
(B)
c*r, up to four levels deep.
list: build list all at once
1 有用 松志 2014-08-13 00:17:25
牛逼的语言,牛逼的著作,值得收藏!
0 有用 蝉 2014-03-13 19:49:23
:无
0 有用 akito 2012-06-23 20:50:17
里面的例子写的很有诚意,读过的最有趣的编程书了
0 有用 [已软注销] 2011-08-03 15:20:50
以编写一个游戏来讲解Lisp,如果从没学过Lisp建议不要先看这书,先看Paul Graham的ANSI Common LISP
0 有用 一寸行者 2014-02-11 18:17:20
Learn lisp the hard way via creating text games.
0 有用 ◾️ 2021-01-02 14:06:43
这书讲得贼慢 但是很可爱 好奇有人会用lisp入门吗? @2019-03-04 17:51:45
0 有用 longbo567 2020-10-16 18:20:36
例子有趣.烧脑
0 有用 al_lea 2019-04-18 08:29:51
对Lisp满满的热爱;其实写的有点拧巴,作者在Hacker news上也承认,原来是写本高阶书的,结果出版商倾向于入门,于是;对于大型例子的介绍总是忽略了数据结构的设计部分,看例子很晕。其他就都是优点了。
0 有用 Ziv 2019-03-04 14:15:50
并不有趣。其实很无聊。
0 有用 JoeBlack220 2018-10-27 12:14:49
深入浅出诶,适合当第一本lisp入门书