;; usefuls.scm ;; Useful functions and examples for final projects ;; Input-loop defines a simple user input cycle ;; that can easily be extended to whatever purpose ;; you'd like. (define (input-loop) (display "> ") (let ((user-input (read-line))) (cond ((null? user-input) (input-loop)) ((equal? (car user-input) 'quit) (show "Bye!")) (else (begin (display "User input was: ") (display user-input) (newline) (input-loop)))))) ;; Returns the hexadecimal string representation of ;; the color defined by the three integers as argument ;; (create-color 255 0 0) is pure red, ;; (create-color 255 0 255) is pure purple, et cetera. (define (create-color red green blue) (define (hex-digit num) (let ((str-rep (number->string num 16))) (if (= (string-length str-rep) 1) (string-append "0" str-rep) str-rep))) (define (bound num) (min 255 (max num 0))) (let ((red (bound red)) (blue (bound blue)) (green (bound green))) (apply string-append (cons "#" (map hex-digit `(,red ,green ,blue))))))