这是我对Lisp程序的第二次正确尝试,它是Mythender (一个免费分发的桌面RPG)的骰子。不过,它也有几个问题:
任何一般性建议也将受到欢迎。
(defpackage :mythender (:add-use-defaults t) (:use "CAPI"))
(in-package :mythender)
(defun d6 () (the fixnum (+ 1 (random 6))))
(defun d6s (count)
(declare (type fixnum count))
(the list (loop for x from 1 to count collecting (d6))))
(defun d6over (count threshold)
(declare (type fixnum count threshold))
(the fixnum (count-if
(lambda (x) (> threshold x))
(d6s count))))
(defvar *storm* 3)
(defvar *thunder* 3)
(defvar *lightning* 0)
(declare (ftype (function) printstate))
(defun printstate ()
(print *storm*)
(print *thunder*)
(print *lightning*)
(the null ()))
(defun roll ()
(incf *lightning* (d6over *thunder* 3))
(incf *thunder* (d6over *storm* 3))
(the null ()))
(defun damage (threshold)
(setf *thunder* (d6over *thunder* threshold))
(the null ()))
(defun doroll (&rest args)
(roll)
(update-interface)
(the null ()))
(define-interface mythender-interface () ()
(:panes
(roll-button push-button :data "Roll" :callback #'doroll)
(damage-button push-button :data "Damage")
(storm-pane display-pane :title "Storm:" :title-position :left)
(thunder-pane display-pane :title "Thunder:" :title-position :Left)
(lightning-pane display-pane :title "Lightning:" :title-position :left))
(:layouts
(main-layout column-layout '(storm-pane thunder-pane lightning-pane buttonlayout))
(buttonlayout row-layout '(roll-button damage-button))))
(defvar *interface*)
(defun update-interface-slot (slotname value)
(declare (type string slotname) (type fixnum value))
(setf (display-pane-text (slot-value *interface* slotname)) (write-to-string value))
(the null ()))
(defun update-interface ()
(update-interface-slot 'storm-pane *storm*)
(update-interface-slot 'thunder-pane *thunder*)
(update-interface-slot 'lightning-pane *lightning*)
(the null ()))
(defun start ()
(setf *interface* (make-instance 'mythender-interface))
(display *interface*)
(the null (update-interface)))发布于 2014-05-11 07:46:04
您的构建问题的答案必须等到您告诉我们构建语句和错误消息。
你的最后一个问题:
(declare (ftype (function) printstate))
(defun printstate ()
(print *storm*)
(print *thunder*)
(print *lightning*)
(the null ()))众所周知,这是一种功能。没必要声明。这样的声明类型,在普通的Common中只具有向编译器提供优化提示的目的,编译器可能会忽略这些提示。实际上,只有CMUCL (以及诸如SBCL和SCL这样的派生编译器)对声明的类型做了更多的工作。
没有人用Lisp编写这样的代码。最好省略那些类型。记住: Lisp不是一种静态类型的语言。
(defun printstate ()
(print *storm*)
(print *thunder*)
(print *lightning*)
(values))使用(values)会导致函数不返回值。这通常是首选的,不返回NIL。
如果您希望在运行时以有意义的方式检查类型,那么请使用ASSERT、CHECK-TYPE和/或DEFMETHOD。
(defun d6s (count)
(declare (type fixnum count))
(the list (loop for x from 1 to count collecting (d6))))只是:
(defmethod d6s ((n integer))
"Returns a list of n dice rolls."
(loop repeat n collect (d6)))不要忘记以人类可读的形式描述函数的语义。
https://stackoverflow.com/questions/23587872
复制相似问题