How do you make an interactive input loop in Racket?

Viewed 2431

I'm trying to make a loop which asks for a command, executes the command, and then loops again. It only exits if the command entered is "exit". I have tried a couple different things, but when I run them they work once before (read-line) reads an # and then does this infinitely without waiting for input. My initial attempt looked like this:

(define (inputLoop)
    (define command "init")
    (do()((equal? command "exit") (display "exited successfully..."))
        (display "What would you like to do?(start,stop,exit)")
        (set! command (read-line))
        (cond [(equal? command "start") (start)]
              [(equal? command "stop") (stop)]
              [else (void)])))

My next attempt was similar, but instead of using a do-loop, it just recursively called the function called inputLoop if the command was not "exit".

Here is a sample of the output:

What would you like to do?(start,stop,exit)start
What would you like to do?(start,stop,exit)
What would you like to do?(start,stop,exit)
...<thousands-of-lines-here>
What would you like to do?(start,stop,exit)
What would you like to do?(start,stop,exit)
What would you like to do?(start,stop,exit)exit
exited successfully...
2 Answers
Related