<div dir="ltr">Hello.<div><br></div><div>I wrote a tiny and silly joking program as below.</div><div><br></div><div>;;;;;;;;;;;;;;;;;;</div><div><br></div><div><div>#lang racket</div><div><br></div><div>(require srfi/48)</div>
<div><br></div><div>;;;;; Data</div><div><br></div><div>(define *script*</div><div>  #hasheq((intro . "Let me help to show you the age of marrage to be happy, Lady!~%~%1. You need only one input later.~%2. If you'd like to step forward, just hit the Enter/Return key.~%~%Do you want to play? [y/n]")</div>
<div>          (first-step . "~%At first, please call a two-digit number whichever you like in your mind.~%Then please hit the Enter/Return key~%~%")</div><div>          (second-step . "Please add the one's digit and the ten's digit in your mind.~%Then please hit the Enter/Return key.~%~%")</div>
<div>          (question . "Is the number still a two-digit number? [y/n] ")</div><div>          (third-step . "~%Next, please multiply the answer you've just got by 9 in your mind.~%Then please hit the Enter/Return key.~%~%")</div>
<div>          (forth-step . "Now, Please add the one's digit and the ten's digit of the answer you've just got in your mind.~%Then please hit the Enter/Return key.~%~%")</div><div>          (fifth-step . "At last, please add the number of guys you've slept with to the answer you've just got in your mind.~%Then please input the answer you've got below.~%==> ")</div>
<div>          (conclusion . "~%You'd better get married when you are ~a years old to have a happy life,~%and that implies you've slept with ~a ~a ~a.~%~%")</div><div>          (only . "only")</div>
<div>          (nothing . "")</div><div>          (man . "man")</div><div>          (men . "men")</div><div>          (play-again? . "Play again?")))</div><div><br></div><div>;;;;; Environment</div>
<div><br></div><div>(define *env* (hasheq 'phase #f 'messages *script*))</div><div><br></div><div>;;;;; Read</div><div><br></div><div>(define (y-or-n?)</div><div>  (let ((i (read)))</div><div>    (let ((sym (string->symbol (string-upcase (substring (symbol->string i) 0 1)))))</div>
<div>      (case sym</div><div>        ((Y) #t)</div><div>        ((N) #f)</div><div>        (else (raise 'not-yes-nor-no))))))</div><div><br></div><div>(define (parser x env)</div><div>  (case (hash-ref env 'phase)</div>
<div>    ((intro question play-again?) (values (y-or-n?) env))</div><div>    ((fifth-step) (let ((i (read)))</div><div>                    (values (if (integer? i)</div><div>                                i</div><div>                                (raise 'input-error)) env)))</div>
<div>    ((first-step</div><div>      second-step</div><div>      third-step</div><div>      forth-step) (read-char) (values x env))  ;;; THIS PART DOESN'T WORK PROPERLY!</div><div>    (else (values x env))))</div><div>
<br></div><div>;;;;; Eval</div><div><br></div><div>(define (interp x env)</div><div>  (case (hash-ref env 'phase)</div><div>    ((intro play-again?) (if x</div><div>                             (values x (hash-set env 'phase 'first-step))</div>
<div>                             (exit)))</div><div>    ((first-step) (values x (hash-set env 'phase 'second-step)))</div><div>    ((second-step) (values x (hash-set env 'phase 'question)))</div><div>    ((question) (values x (hash-set env 'phase (if x</div>
<div>                                                   'second-step</div><div>                                                   'third-step))))</div><div>    ((third-step) (values x (hash-set env 'phase 'forth-step)))</div>
<div>    ((forth-step) (values x (hash-set env 'phase 'fifth-step)))</div><div>    ((fifth-step) (values (- x 9) (hash-set env 'phase 'conclusion)))</div><div>    ((conclusion) (values x (hash-set env 'phase 'play-again?)))</div>
<div>    (else (values x (hash-set env 'phase 'intro)))))</div><div><br></div><div>;;;;; Print</div><div><br></div><div>(define (print x env)</div><div>  (let ((c (hash-ref env 'phase))</div><div>        (s (hash-ref env 'messages)))</div>
<div>    (case c</div><div>      ((intro </div><div>        first-step </div><div>        second-step </div><div>        question</div><div>        third-step</div><div>        forth-step</div><div>        fifth-step</div>
<div>        play-again?) (format #t (hash-ref s c)))</div><div>      ((conclusion) (format #t</div><div>                            (hash-ref s c)</div><div>                            (+ x 9)</div><div>                            (hash-ref s (if (< x 2)</div>
<div>                                            'only</div><div>                                            'nothing))</div><div>                            x </div><div>                            (hash-ref s (if (< x 2)</div>
<div>                                            'man</div><div>                                            'men))))))</div><div>  (flush-output)</div><div>  (values x env))</div><div><br></div><div>;;;;; REPL</div>
<div><br></div><div>(define (main x env)</div><div>  (let-values (((x env) (parser x env)))</div><div>    (let-values (((x env) (interp x env)))</div><div>      (let-values (((x env) (print x env)))</div><div>        (main x env)))))</div>
<div><br></div><div> (main #f *env*)</div></div><div><br></div><div>;;;;;;;;;;;;;;;;;;;;;; THE END OF PROGRAM</div><div><br></div><div>I would like to implement something like "HIT ENTER TO CONTINUE", and I used read-char for it; however it doesn't work as I wished.</div>
<div>It seems like when I hit the enter key, it keeps having something other than #\newline in the stream, and two messages of data are shown at once.</div><div>This is not what I mean.</div><div><br></div><div>I tried using charterm, but as implied, it does not work on Windows and says ttf; therefore I gave up using the library on Windows.</div>
<div><br></div><div>Is there any good idea to implement "HIT ENTER TO CONTINUE"?</div><div><br></div><div>By the way, I'm using Windows 7 and Racket V.5.93.</div><div><br></div><div>Thanx.</div></div>