;;; input.ss ;;; A library of routines for getting input from the user. ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 1.1 of October 2000 ;;; Contents: ;;; Input Routines ;;; Helper Routines ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Input Routines ;;; Read a positive number from the standard input port. ;;; Parameters: ;;; A prompt ;;; Preconditions: ;;; The standard input port is available for reading. ;;; Postconditions: ;;; Reads some input. ;;; Writes some output (particularly the prompt). ;;; Returns: ;;; A positive number. (define read-positive-number (lambda (prompt) (display prompt) (let ((val (read))) (if (number? val) val (begin (display "That is not a number. ") (newline) (read-positive-number)))))) ;;; Read a string from the standard input port. If the user enters ;;; something other than a string, try to treat it as a string. ;;; Parameters: ;;; A prompt. ;;; Preconditions: ;;; The standard input port is available for reading. ;;; Postconditions: ;;; Reads some input. ;;; Writes some output. ;;; Returns: ;;; A string. (define read-string (lambda (prompt) (display prompt) (make-string (read)))) ;;; Read one of a list of symbols. If the user enters something ;;; other than one of the valid symbols, tries again. ;;; Parameters: ;;; A non-empty list of valid symbols. ;;; Preconditions: ;;; The list is not empty. ;;; The list contains only symbols. ;;; The standard input port is available for reading. ;;; Postconditions: ;;; Reads some input. ;;; Writes some output. ;;; Returns: ;;; One of the symbols in the list (as specified by ;;; the user). ;;; (define read-symbol (lambda (prompt valid) (display prompt) (display valid) (display " ") (let ((sym (read))) (if (member sym valid) sym (begin (display sym) (display " is not a valid response. ") (newline) (read-symbol prompt valid)))))) ;;; Read a list of strings. ;;; Parameters: ;;; A prompt. ;;; A string that terminates input. ;;; Preconditions: ;;; Both parameters are strings. ;;; The standard input port is available for reading. ;;; Postconditions: ;;; Reads some input. ;;; Writes some output (particularly the prompt). (define read-string-list (lambda (prompt terminator) (let ((val (read-string prompt))) (if (equal? val terminator) null (cons val (read-string-list prompt terminator)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Other utilities ;;; Turn something read into a string (because the typical input is ;;; a symbol rather than a string). ;;; Parameters: ;;; A number, string, or symbol. ;;; Preconditions: ;;; The paremeter is a number, string, or symbol. ;;; Postconditions: ;;; Returns the string equivalent of the parameter. ;;; Does not affect the parameter. (define make-string (lambda (stuff) (cond ((string? stuff) stuff) ((symbol? stuff) (symbol->string stuff)) ((number? stuff) (number->string stuff)) (else "*UNKNOWN*")))) ;;; Capitalize the first letter of a string. ;;; Parameters: ;;; A string ;;; Precondition: ;;; The string is nonempty and contains no spaces. ;;; Postcondition: ;;; Returns the stame string with the first letter capitalized. (define capfirst (lambda (str) (let ((firstchar (string-ref str 0)) (rest (substring str 1 (string-length str)))) (string-append (string (char-upcase firstchar)) rest))))