0%

用Common Lisp写一个双色球程序

断断续续的看了些Common Lisp的东西,主要是田春冰河翻译的那本《实用Common Lisp编程》,另外网上有人翻译了《ANSI Common Lisp》也有看,另一本《Common Lisp: A Gentle Introduction to Symbolic Computation》。总得写点东西,于是就写了这个双色球的小程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(defconstant total-red-ball 33)
(defconstant total-blue-ball 16)

(defun select-red-ball (red-balls double-balls)
(let (x)
(setq x (elt red-balls (random (length red-balls))))
(vector-push x double-balls)
(delete x red-balls)))

(defun create-double-balls()
(let ((red-balls (make-array total-red-ball :fill-pointer 0))
(blue-balls (make-array total-blue-ball :fill-pointer 0))
(double-balls (make-array 7 :fill-pointer 0)))
(loop for i from 1 to total-red-ball do (vector-push i red-balls))
(loop for i from 1 to total-blue-ball do (vector-push i blue-balls))
(dotimes (n 6)
(select-red-ball red-balls double-balls))
(setq double-balls (sort double-balls #'<))
(vector-push (elt blue-balls (random 16)) double-balls)
(print double-balls)))

(defun gen(n)
(dotimes (i n)
(create-double-balls)))

Lisp特殊的语法风格确实与C体系的语言有很大不同,不过如果有编程经验的话,还是比较容易上手,比如函数的传参,传个数组的话,实际上传指针了。定义局部变量的方式比如用LET