aboutsummaryrefslogtreecommitdiff
path: root/mathslap-rec/mathslap-rec.clisp
diff options
context:
space:
mode:
authorGene Pasquet <dev@etenil.net>2026-07-30 20:15:58 +0100
committerGene Pasquet <dev@etenil.net>2026-07-30 20:15:58 +0100
commite426b6afac91ba4088d0023127e5f65fad943414 (patch)
treefc0e32211290b5582a63f4b19a942226e1532a47 /mathslap-rec/mathslap-rec.clisp
parent99ed9e86fe5aeeceba43ae45034ee6ac83a75921 (diff)
Rework, use let and more idiomatic code
Diffstat (limited to 'mathslap-rec/mathslap-rec.clisp')
-rw-r--r--mathslap-rec/mathslap-rec.clisp51
1 files changed, 29 insertions, 22 deletions
diff --git a/mathslap-rec/mathslap-rec.clisp b/mathslap-rec/mathslap-rec.clisp
index c09e3c2..86d607b 100644
--- a/mathslap-rec/mathslap-rec.clisp
+++ b/mathslap-rec/mathslap-rec.clisp
@@ -6,29 +6,36 @@
(print "MathSlap")
(print "Hit Enter for another number. q and Enter to quit.")
-(defparameter *line* "")
-(defparameter *out* "MathSlap ")
-(defparameter *random-state* 0)
(setq *random-state* (make-random-state t))
+;;;; Generate a random number for our game. Avoid having to write the
+;;;; same code several times.
+(defun have-number ()
+ (+ (random 10) 1))
+
;;;; append string parameter with a random number 1 through 10 and a space
-(defun concat (o)
- (concatenate 'string o (write-to-string (+ (random 10) 1)) " ")
-)
-
-(defun recloop (o)
-;;;; append o with a random number 1 through 10 and a space
- (setq o (concat o))
- (print o)
-;;;; wait for input
- (setq *line* (read-line *standard-input*))
- (if (string= *line* "q")(quit))
- (recloop o)
-)
-
-;;;; Add numbers to output string. Comment one of these out to start with 2 numbers.
-(setq *out* (concat *out*))
-(setq *out* (concat *out*))
-;;;; recursive loop
-(recloop *out*)
+(defun extend-prompt (o)
+ (concatenate 'string o (write-to-string (have-number)) " "))
+
+;;;; Same but with format, possibly more readable?
+;; (defun extend-prompt (o)
+;; (format nil "~a~d " o (have-number)))
+
+(defun recloop (o input)
+ ;; Only recurse if the input is not "q"
+ (unless (string= input "q")
+ ;; Deal with the current state: output the prompt
+ (print o)
+ (finish-output)
+ ;; Prepare the next prompt and collect input.
+ ;; Use a LET form to use local variables
+ (let ((prompt (extend-prompt o))
+ (new-input (read-line *standard-input*)))
+ (recloop prompt new-input))))
+
+;;;; Initiate the recursion
+;; Use a LET* form for local variables, with defined variables depending on each other.
+(let* ((starting-numbers (list (have-number) (have-number)))
+ (starting-prompt (format nil "MathSlap ~{~a ~}" starting-numbers)))
+ (recloop starting-prompt nil))