;; Solution to the find words problem ;; Copyright 1999 Paul Twohey. All rights reserved ;; the list of words (define dictionary (sentence 'mouse 'cat 'rat)) ;; the procedure to help Jane solve her crossword puzzle (define (find-words word-length start-position letters) ;; the helper function length-filter takes a sentence (list) ;; of words an returns a list of words of length word-length (define (length-filter words) (cond ((null? words) nil) ((= (count (first words)) word-length) (sentence (first words) (length-filter (bf words)))) (else (length-filter (bf words))))) ;; the helper function letter-filter takes a position, a letter and a ;; sentence (list) of words and returns a list of words who have the ;; letter at the position. This function assumes that all the words ;; are long enough (define (letter-filter position letter words) ;; the helper function letter-at takes a word and a position ;; and returns a one character word that is the letter at the ;; desired postion. This function assumes that all the words ;; are enough (define (letter-at wrd position) (if (= position 1) (first wrd) (letter-at (bf wrd) (- position 1)))) (cond ((null? words) nil) ((equal? (letter-at (first words) position) letter) (sentence (first words) (letter-filter position letter (bf words)))) (else (letter-filter position letter (bf words))))) ;; the helper function letter-reducer takes a list of letters, ;; a position, and a senctence (list) of words and returns a ;; list of words that have the letter sequence starting at position ;; with the letters in the list of letters (define (letter-reducer letters position words) (cond ((null? words) words) ((null? letters) words) (else (letter-reducer (bf letters) (+ position 1) (letter-filter position (first letters) words))))) (letter-reducer letters start-position (length-filter dictionary)))