12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- (import
- (except (rnrs base) let-values)
- (only (guile)
- lambda* λ)
- (ice-9 textual-ports))
- ;; Probably could improve here, by using SRFI uniform random
- ;; integer generation, but this is a toy example.
- (define max-guesses 10)
- (define max-target 100)
- (define target (random max-target))
- ;; Define read-line or use whatever your Scheme dialect
- ;; offers.
- (define read-line
- (lambda* (#:optional (input-port (current-input-port)))
- (get-line input-port)))
- (let loop ([guesses-made 0])
- ;; Output a prompt in whatever way your Scheme dialect
- ;; does it.
- (display
- (simple-format
- #f "Type a number between 0 and ~a: " max-target))
- (cond
- [(< guesses-made max-guesses)
- (let ([guess (string->number (read-line))])
- (display (simple-format #f "You guessed: ~a.\n" guess))
- (cond
- [(= guess target)
- (display (simple-format #f "You win!\n"))]
- [(< guess target)
- (display (simple-format #f "Too small! Try again!\n"))
- (loop (+ guesses-made 1))]
- [else
- (display (simple-format #f "Too big! Try again!\n"))
- (loop (+ guesses-made 1))]))]
- [else
- (display (simple-format #f "All out of guesses. You lose!\n"))]))
|