fork download
  1. #!/usr/bin/env racket
  2. #lang racket
  3.  
  4. ;; メッセージ
  5. (define *message* (hasheq 'opening "Guess the number!"
  6. 'prompt "Please input your guess."
  7. 'failure "Failed to read line"
  8. 'guess "You guessed: "
  9. #t "Too small!"
  10. #f "Too big!"
  11. 'win "You win!"))
  12.  
  13. ;; 環境
  14. (struct World (guess secret-number ordering) #:transparent)
  15.  
  16. ;; 初期化
  17. (define (init mx)
  18. (println (hash-ref *message* 'opening))
  19. (World +inf.0 (random 1 (add1 mx)) null))
  20.  
  21. ;; Read
  22. (define (input prompt)
  23. (display prompt)
  24. (string->number (read-line)))
  25.  
  26. ;; Eval
  27. (define (world-go x env)
  28. (match-let (((World guess secret-number ordering) env))
  29. (if (= x secret-number)
  30. (raise 'win #t)
  31. (World x secret-number (< x secret-number)))))
  32.  
  33. ;; Print
  34. (define (print env)
  35. (printf "~a~a~%~a~%"
  36. (hash-ref *message* 'guess)
  37. (World-guess env)
  38. (hash-ref *message* (World-ordering env)))
  39. env)
  40.  
  41. ;; Main関数(REPL)
  42. (module+ main
  43. (let loop ((w (init 100)))
  44. (with-handlers ((symbol?
  45. (lambda (key)
  46. (printf "~a~%" (hash-ref *message* key))
  47. (exit)))
  48. (exn:fail:contract?
  49. (lambda (e)
  50. (printf "~a~%" (hash-ref *message* 'failure))
  51. (loop w))))
  52. (loop (print(world-go (input (hash-ref *message* 'prompt)) w))))))
  53.  
Success #stdin #stdout 0.6s 114916KB
stdin
Standard input is empty
stdout
Standard output is empty