main.scm 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (import
  2. (except (rnrs base) let-values)
  3. (only (guile)
  4. lambda* λ)
  5. (ice-9 textual-ports))
  6. ;; Probably could improve here, by using SRFI uniform random
  7. ;; integer generation, but this is a toy example.
  8. (define max-guesses 10)
  9. (define max-target 100)
  10. (define target (random max-target))
  11. ;; Define read-line or use whatever your Scheme dialect
  12. ;; offers.
  13. (define read-line
  14. (lambda* (#:optional (input-port (current-input-port)))
  15. (get-line input-port)))
  16. (let loop ([guesses-made 0])
  17. ;; Output a prompt in whatever way your Scheme dialect
  18. ;; does it.
  19. (display
  20. (simple-format
  21. #f "Type a number between 0 and ~a: " max-target))
  22. (cond
  23. [(< guesses-made max-guesses)
  24. (let ([guess (string->number (read-line))])
  25. (display (simple-format #f "You guessed: ~a.\n" guess))
  26. (cond
  27. [(= guess target)
  28. (display (simple-format #f "You win!\n"))]
  29. [(< guess target)
  30. (display (simple-format #f "Too small! Try again!\n"))
  31. (loop (+ guesses-made 1))]
  32. [else
  33. (display (simple-format #f "Too big! Try again!\n"))
  34. (loop (+ guesses-made 1))]))]
  35. [else
  36. (display (simple-format #f "All out of guesses. You lose!\n"))]))