【发布时间】:2018-03-29 17:58:40
【问题描述】:
我是 c 的初学者。 Lisp,实际上我的第一个代码有一些问题。主题是一个带有随机数的“骰子游戏”。如果你得到两个 1,你就赢了,如果你得到两个 6,你也赢了。
首先我写下这段代码:
(defun dice ()
(+ (random 6) 1))
(defun dicegame (dice dice)
(and (and (or
(equal (print (dice)) 1)
(equal (print (dice)) 6))
(or
(equal (print (dice)) 1)
(equal (print (dice)) 6)))
(or (equal (print (dice)) (print (dice))))))
(dicegame (dice) (dice))
它不工作。我得到了 6 个数字的结果,这些数字是 nil,我得到了 ex 的结果。 3 5 4 5 3 4 这是真的。我不明白。
比我写了一个正在工作的新的:
(defun dicegame-s (dice dice)
(or (and (equalp (print (dice)) 1)
(equalp (print(dice)) 1))
(and (equalp (print(dice)) 6)
(equalp (print(dice)) 6))))
我的问题是:为什么第一个不工作而第二个工作。
谢谢!
【问题讨论】:
-
如果你修正你的缩进,事情可能会变得很明显。 Emacs 可以提供帮助。
-
在你的函数 DICEGAME 上:它有两个同名的参数。那有意义吗?此外,这些参数未使用。相反,您反复 (骰子) 。你认为(骰子)有什么作用?
-
定义“工作”?
-
不要将输出与计算混合。首先计算你的值,然后打印它们(或者只在你的 REPL 中查看它们而不打印)。
标签: random lisp common-lisp dice