diff --git a/README.md b/README.md index bf178da..d48d124 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Keys in watch mode (each key is a line: type it, then press ENTER): | key | action | | --- | ------------------------------------ | | n | move to the next pending exercise | -| h | show the hint, pinned on the screen | +| h | reveal the next hint paragraph (stays pinned) | | r | rerun the current exercise | | l | list all exercises | | q | save progress and quit | @@ -78,7 +78,7 @@ solved. ```sh ./lisplings watch the current exercise (the default) ./lisplings run [name] run one exercise and print its output -./lisplings hint [name] show the hint for an exercise +./lisplings hint [name] print every hint paragraph, numbered ./lisplings list [pending|done|all] ./lisplings reset restore an exercise to its pristine state ./lisplings check-all run every exercise and resync progress diff --git a/info.sexp b/info.sexp index 9c0083f..525c1df 100644 --- a/info.sexp +++ b/info.sexp @@ -3,7 +3,8 @@ ;;; Read by the lisplings runner. Each exercise is a plist: ;;; :name the exercise name; file is exercises//.lisp ;;; :dir the chapter directory -;;; :hint shown by `lisplings hint ' +;;; :hint one hint paragraph, or a list of them; watch mode reveals +;;; one per `h', `lisplings hint ' prints them all ((:welcome "Welcome to Lisplings! @@ -30,7 +31,7 @@ Found an issue, or want to add an exercise? Improvements welcome!") (:exercises ((:name "intro1" :dir "00_intro" - :hint "This exercise has no errors. Play with the banner, save the file, and press n in the terminal. You might need to press ENTER after typing n.") + :hint ("This exercise has no errors. Play with the banner, save the file, and press n in the terminal. You might need to press ENTER after typing n.")) (:name "intro2" :dir "00_intro" :hint "The format directive ~a expects a value from the arguments after the format string. The string below has no arguments at all...") @@ -51,7 +52,9 @@ Found an issue, or want to add an exercise? Improvements welcome!") :hint "Match the parameter list to the calls: average is called with TWO numbers here. And (- a b) subtracts b from a - check which way round you wrote it.") (:name "functions3" :dir "02_functions" - :hint "&OPTIONAL gives a parameter a default: (defun repeat-word (word &optional (n 2)) ...). For the box, a keyword default may use an earlier parameter: (defun make-box (width &key (height width) (depth width)) ...).") + :hint ("&OPTIONAL gives a parameter a default: (defun repeat-word (word &optional (n 2)) ...)." + "A keyword default may use an earlier parameter: (defun make-box (width &key (height width) (depth width)) ...)." + "Further reading: CLHS 3.4.1 (ordinary lambda lists) and PCL ch. 5, https://gigamonkeys.com/book/functions.html")) (:name "functions4" :dir "02_functions" :hint "FLOOR returns two values at once: (floor 10 3) is 3 and 1. Pass it straight through. For 125 seconds: (floor seconds 60) is 2 minutes and 5 seconds.") @@ -234,7 +237,8 @@ Found an issue, or want to add an exercise? Improvements welcome!") :hint "MULTIPLE-VALUE-BIND on GETHASH: if PRESENT return the value, otherwise compute, SETF the table, return. The second call must not touch SLOW-FIB again.") (:name "quiz1" :dir "quizzes" - :hint "APPEND makes a new book with the contact at the end. DOLIST + STRING-EQUAL + GETF finds by name; FORMAT makes the card.") + :hint ("APPEND makes a new book with the contact at the end." + "DOLIST + STRING-EQUAL + GETF finds by name; FORMAT makes the card.")) (:name "quiz2" :dir "quizzes" :hint "A COND over three shapes: numbers, quote forms, and operator forms. MEMBER (car expr) '(+ - *) recognizes the operators; APPLY + MAPCAR evaluates the arguments.")))) diff --git a/src/core.lisp b/src/core.lisp index f47d013..d0d3189 100644 --- a/src/core.lisp +++ b/src/core.lisp @@ -65,6 +65,16 @@ ;; terminal; tell it whether the parent is showing color. (sb-posix:setenv "LISPLINGS_COLOR" (if *use-color* "always" "never") 1)) +(defun normalize-hint (raw name) + "Normalize a :hint catalogue entry to a list of paragraph strings. +A single string becomes a one-element list, a list of strings passes +through, anything else fails fast at startup." + (cond ((null raw) '()) + ((stringp raw) (list raw)) + ((and (listp raw) (every #'stringp raw)) (copy-list raw)) + (t (error "Bad :hint for ~a: expected a string or list of strings" + name)))) + (defun make-exercises () (mapcar (lambda (spec) (let ((name (getf spec :name)) @@ -72,7 +82,7 @@ (make-exercise :name name :dir dir - :hint (getf spec :hint) + :hint (normalize-hint (getf spec :hint) name) :done (and (member name (getf *state* :done) :test #'string=) t) :path (merge-pathnames (format nil "exercises/~a/~a.lisp" dir name) *project*) @@ -283,17 +293,24 @@ Returns (values output-string exit-code timed-out-p)." (nth (screen-state-index state) *exercises*)) (defun hint-paragraphs (ex) - "The hint as a list of paragraph strings. The catalogue gives either -a plain string or a list of strings." - (let ((hint (exercise-hint ex))) - (cond ((null hint) '()) - ((stringp hint) (list hint)) - ((and (listp hint) (every #'stringp hint)) hint) - (t (error "Bad :hint for ~a: expected a string or list of strings" - (exercise-name ex)))))) + "The hint paragraphs; make-exercises normalizes :hint to a list." + (exercise-hint ex)) (defun hint-count (ex) - (length (hint-paragraphs ex))) + (length (exercise-hint ex))) + +(defun reveal-next-hint (state) + "Reveal one more hint paragraph for the current exercise, or +explain why that is not possible." + (let ((total (hint-count (current-exercise state)))) + (cond ((zerop total) + (setf (screen-state-notice state) + "No hint for this one. You're on your own!")) + ((< (screen-state-revealed state) total) + (incf (screen-state-revealed state))) + (t + (setf (screen-state-notice state) + "No more hints for this exercise."))))) (defun valid-keys (state) "The keys that make sense right now, as key-and-label strings." @@ -519,10 +536,9 @@ the queue. Returns NIL at EOF." (t (setf (screen-state-result state) :alldone) (render-screen state)))))))) - ((string-equal cmd "h") - (setf (screen-state-revealed state) - (hint-count (current-exercise state))) - (render-screen state)) + ((string-equal cmd "h") + (reveal-next-hint state) + (render-screen state)) ((or (string-equal cmd "r") (string= cmd "")) (run-and-show state queue lock)) ((string-equal cmd "l")