[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

http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-34.html#node_sec_27.2

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)
  (cond
    [(empty? afile) empty]
    [else (local ((define NEWLINE 'NL)

                  ;;process-file: (symbol [line] -> [line]) [line] [symbol] 
-> [line]
                  (define (process-file combiner onEOL afile)
                    (cond
                      [(empty? afile) empty]
                      [else (cond
                              [(symbol=? (first afile) NEWLINE) 
*****onEOL******]
                              [else (****combiner***** (first afile) 
(process-file f
                                                                     (if 
(empty? onEOL)
                                                                         
empty
                                                                         
(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) 
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 
(process-file).

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 
anything...........




Posted on the users mailing list.