[plt-scheme] Useless abstraction? HtDP 27.2.2

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat May 15 11:04:07 EDT 2010

On May 12, 2010, at 3:52 PM, Horace Dynamite wrote:

> I get correct output for my example input, but the
> definition seems more complicated than the original.

The use of any abstraction introduces a form of 
complexity into a program. It is often a bit more 
difficult to read and comprehend a program that 
uses abstractions. HOWEVER, this small cost almost
aways pays off when you need to change your program
later on. Promised, I encounter this all the time. 

Independently of complexity programmers create
convoluted organizations. That's what happened in
your case, and it's mostly because you seem to 
lack practice in abstracting. 

Practice means doing little exercises like this one
first and lots of them. This experience internalizes 
the design recipe for abstraction and makes abstraction
so second nature that the above-mentioned costs

On to your concrete example: The purpose of the 
exercise is to abstract over first-line and 
remove-first-line. What your solution shows is that
you failed to understand that the recursion a part
of the pattern not a part of the special purpose. 
In other words, your extract-from-file function 
doesn't capture all commonalities between the
two functions. If it did, you'd be able to write: 

(define (first-line afile) (f afile (lambda (x) empty) cons))
(define (remove-first-line afile) (f afile rest (lambda (a d) d)))

;; --- 

A couple of additional recommendations: 

1. Abstract before you use local to reorganize your program. 

2. Use test cases for file->list-of-lines, like this: 

(check-expect (file->list-of-lines (list 'a 'b NEWLINE 'c)) '((a b) (c)))

3. Rename the old functions by adding a .v1 when you define
versions based on the common abstraction f. 

Good patient, thinking (not luck)  -- Matthias

Posted on the users mailing list.