How do you create an interactive menu in scheme

534 views Asked by At

I have recently been given the task to create a program that requires one to make use of a menu. However, I have no idea how to create a window and make it display text that can be interacted with using a certain key, let's say the 'enter' key. Does anyone have any hints on how to do this?

1

There are 1 answers

0
Sylwester On

Scheme reports don't have GUI support so the only portable would be a CLI interface. This is a very simple program that just has these parts.

#!r6rs
(import (rnrs))

(define *stdin* (current-input-port))
(define (readline)
  (get-line *stdin*))

;;; displays a textual menu
(define (menu)
  (display "Menu\n1. read input\n2. print data\n3. empty input\n"))

;; read until we got the value between 1 and 3 from user
(define (read-command)
  (display "Enter choice [1-3] >")
  (let* ((in (readline))
         (n (string->number in)))
    (cond ((<= 1 n 3) n)
          (else
           (display "Invalid choice \"")
           (display in)
           (display "\"\n")
           (read-command)))))


(define (driver data)
  (menu)
  (let ((choice (read-command)))
    (cond ((= choice 1) (display "Enter text >")
                        (driver (cons (readline) data)))
          ((= choice 2) (display "Data:\n")
                        (display data)
                        (newline)
                        (driver data))
          (else (display "Emptied\n")
                (driver '())))))

(driver '())

Of course individual implementations has GUI support. Eg. Racket has a way create desktop applications where the menu can be buttons you click.