;;; Procedure: ;;; in-tree? ;;; Parameters: ;;; sym, a symbol ;;; tree, a symbol tree ;;; Purpose: ;;; Determine if sym appears in tree. ;;; Produces: ;;; appears, a Boolean value ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; appears is true if sym appears somewhere in tree ;;; appears is false otherwise. (define in-tree? (lambda (sym tree) (or (eqv? sym tree) (and (pair? tree) (or (in-tree? sym (car tree)) (in-tree? sym (cdr tree))))))) ;;; Procedure: ;;; list-of-symbols->string ;;; Parameters: ;;; lst, a list of symbols ;;; Purpose: ;;; To convert lst to a string in "normal" Scheme list format ;;; (open paren, values separated by spaces, close paren). ;;; Produces: ;;; str, a string ;;; Preconditions: ;;; [Standard] ;;; Postconditions: ;;; str begins with an open paren. ;;; str ends with a close paren. ;;; All the elements of lst "appear" in str in the same order. ;;; No other values appear in str. ;;; Each pair of elements in lst are separated by a space. (define list-of-symbols->string (lambda (lst) ; The kernel handles all but the first symbol. (letrec ((kernel (lambda (lst) (if (null? lst) "" (string-append " " (symbol->string (car lst)) (kernel (cdr lst))))))) (if (null? lst) "()" (string-append "(" (symbol->string (car lst)) (kernel (cdr lst)) ")"))))) (define los->string (lambda (lst) (letrec (; Convert the list to a string with spaces between elements ; but no parens (kernel (lambda (lst) ; Base case: Singleton list (if (null? (cdr lst)) ; If the list has only one element, the string version ; (w/o parens) is .. (symbol->string (car lst)) ; Otherwise (string-append (symbol->string (car lst)) " " (kernel (cdr lst))))))) (if (null? lst) "()" (string-append "(" (kernel lst) ")"))))) (define number->digits (lambda (val) (letrec ((kernel (lambda (val) (if (zero? val) null (cons (remainder val 10) (kernel (quotient val 10))))))) (if (zero? val) (list 0) (reverse (kernel val))))))