## 我是来看common lisp效果的 原

yyliu

``````;;;The extent of the world
(defparameter *width* 100)
(defparameter *height* 30)
(defparameter *jungle* '(45 10 10 10))
(defparameter *plant-energy* 80)
;;;Growing plants in our world
;;cons cells should be compared with equal
(defparameter *plants* (make-hash-table :test #'equal))
;;Grow new plants
(defun random-plant (left top width height)
(let ((pos (cons (+ left (random width)) (+ top (random height)))))
(setf (gethash pos *plants*) t)))
(apply #'random-plant *jungle*)
(random-plant 0 0 *width* *height*))
;;;Creating animals
(defstruct animal x y energy dir genes)
;;x,y stand for the position
;;energy represent its energy,when the energy exhausted,it will die
;;dir is the direction it faced
;;genes decide the direction it will choose
;;Creating an animal in the center of the map
(defparameter *animals*
(list (make-animal :x;we only use list to traverse animal,
;its efficient enough.
(ash *width* -1)
:y
(ash *height* -1)
:energy
1000
:dir
0
:genes;genes represent the possiblity
;that an animal will choose
(loop repeat 8
;collect is OK??
collect (1+ (random 10))))))
;;;Handling animal motion
;;;想想九宫格
(defun move (animal)
(let ((dir (animal-dir animal))
(x (animal-x animal))
(y (animal-y animal)))
;如果达到*width*,置0
(setf (animal-x animal) (mod (+ x
(cond ((and (>= dir 2) (< dir 5)) 1)
((or (= dir 1) (= dir 5)) 0)
(t -1))
*width*)
*width*))
(setf (animal-y animal) (mod (+ y
(cond ((and (>= dir 0) (< dir 3)) -1)
((and (>= dir 4) (< dir 7)) 1)
(t 0))
*height*)
*height*))
(decf (animal-energy animal))))
;;;Handling animal turning
(defun turn (animal)
(let ((x (random (apply #'+ (animal-genes animal)))))
;;this was not easy to understand it,
;;当随机数落在哪个区间就哪个方向的递归描述
(labels ((angle (genes x)
(let ((xnu (- x (car genes))))
(if (< xnu 0)
0
(1+ (angle (cdr genes) xnu))))))
(setf (animal-dir animal)
(mod (+ (animal-dir animal) (angle (animal-genes animal) x))
8)))))
;;;Handling animal eating
(defun eat (animal)
(let ((pos (cons (animal-x animal) (animal-y animal))))
(when (gethash pos *plants*)
(incf (animal-energy animal) *plant-energy*)
(remhash pos *plants*))))
;;;Handling animal reproduction
;;定义繁殖时需要能量
(defparameter *reproduction-energy* 200)
(defun reproduce (animal)
(let ((e (animal-energy animal)))
(when (>= e *reproduction-energy*)
(setf (animal-energy animal) (ash e -1))
(let ((animal-nu (copy-structure animal));浅复制命令
(genes (copy-list (animal-genes animal)))
(mutation (random 8)))
(setf (nth mutation genes)
(max 1 (+ (nth mutation genes) (random 3) -1)))
;This means the gene value will change plus or minus one, or
;stay the same.
(setf (animal-genes animal-nu) genes)
(push animal-nu *animals*)))))
;;;Simulating a day in our world
(defun update-world ()
(setf *animals* (remove-if (lambda (animal)
(<= (animal-energy animal) 0))
*animals*))
(mapc (lambda (animal)
(turn animal)
(move animal)
(eat animal)
(reproduce animal))
*animals*)
;;;Drawing our world
;;;This has low performance but will not matters
(defun draw-world ()
(loop for y
below *height*
do (progn
(fresh-line);outputs a newline only if the output-stream
;is not already at the start of a line
(princ "|")
(loop for x
below *width*
do (princ (cond ((some (lambda (animal)
;可能不止一个动物
(and (= (animal-x animal) x)
(= (animal-y animal) y)))
*animals*)
#\M)
((gethash (cons x y) *plants*) #\*)
(t #\space))))
(princ "|"))))
;;;Creating a user interface
(defun evolution ()
(draw-world)
(fresh-line)
(cond ((equal str "quit") ())
;Recall Conrad’s Rule of Thumb for Comparing Stuff
;use eq for symbols
;use equal for everything else
(t (let ((x (parse-integer str :junk-allowed t)))
(if x
(loop for i
below x
do (update-world)
if (zerop (mod i 1000))
do (princ #\.))
(update-world))
(evolution))))))``````

### yyliu

FreeBlues
2016/01/20
122
15
LispBox 集成开发环境分析 （一）Windows版本分析

LispBox 集成开发环境分析 （一）Windows版本分析 LispBox 是一个开源的LISP 集成开发环境，由 SLIME (The Superior Lisp Interaction Mode for Emacs) 交互接口、 Quicklisp 库管理器、Clo...

FreeBlues
2012/11/14
0
0
Common Lisp学习资源整理

Lisp Hackers: Interviews with 100x More Productive Programmers Posted on June 26th, 2013 Lisp Hackers: Interviews with 100x More Productive Programmers, by Vsevolod Dyomkin, is ......

2017/01/24
0
0

FreeBlues
2014/02/17
0
1

【IT168 技术文档】甫于日前落幕的Software Development 2.0研讨会，来宾之一的Andrei Alexandrescu被问到未来编程语言的趋势时，他认为函数编程(Functional Pogramming)可能会再度兴起。我认...

2010/12/14
0
0

mybatis缓存的装饰器模式

12
0

imbiao

3
0

sharelocked

5
0
burpsuite无法抓取https数据包

1.将浏览器和burpsuite的代理都设置好 2.在浏览器地址栏输入： http://burp 3.下载下面的证书，并将证书导入浏览器 cacert.der

Frost729

3
0
JeeSite4.x 消息管理、消息推送、消息提醒

ThinkGem

9
0