[plt-scheme] HTDP 27.2.2 - Have I fudged it again?

From: wooks . (wookiz at hotmail.com)
Date: Wed Jul 26 21:06:25 EDT 2006


Here is draft solution to this problem with abstractions highlighted by 

;; file->list-of-lines : file  ->  [[symbol]]
;; to convert a file into a list of lines
(define (file->list-of-lines afile)
    [(empty? afile) empty]
    [else (local ((define NEWLINE 'NL)

                  ;;process-file: (symbol [line] -> [line]) [line] [symbol] 
-> [line]
                  (define (process-file combiner onEOL afile)
                      [(empty? afile) empty]
                      [else (cond
                              [(symbol=? (first afile) NEWLINE) 
                              [else (****combiner***** (first afile) 
(process-file f
(empty? onEOL)
(rest onEOL)) (rest afile)))])]))

                  ;; first-line : file  ->  [symbol]
                  ;; to compute the prefix of afile up to the first 
occurrence of NEWLINE
                  (define first-line (process-file cons
                                                   empty afile))

                  ;; remove-first-line : file  ->  [symbol]
                  ;; to compute the suffix of afile behind the first 
occurrence of NEWLINE
                  (define remove-first-line (process-file (lambda (arg1 
arg2) arg2)
                                                          (rest afile) 

            (cons first-line
                  (file->list-of-lines remove-first-line)))]))

(file->list-of-lines (list 'a 'b 'c 'NL 'd 'e 'NL 'f 'g 'h 'NL))

The areas of concern are the lambda expression in remove-first-line and the 
if expression embedded in the recursive call of the abstract function 

This was what was required after following the initial design recipe steps, 
but it always gets a bit leery when you have to start putting ifs in your 
abstractions. Now I could and in fact will argue that since the parameter 
concerned is a list it is perfectly acceptable to say - well a list could be 
empty or not hence an if is justified therefore I have not fudged 

Posted on the users mailing list.