[plt-scheme] Help With Abstraction - HtDP 27.2.2
On Tue, May 5, 2009 at 12:27 AM, David Yrueta <dyrueta at gmail.com> wrote:
> Hi All --
>
> The task of HtDP exercise 27.2.2 is to abstract the two functions
> below into a single function:
>
> ;; first-line : file -> (listof symbol)
> ;; to compute the prefix of afile up to the first occurrence of NEWLINE
> (define (first-line afile)
> (cond
> [(empty? afile) empty]
> [else (cond
> [(symbol=? (first afile) NEWLINE) empty]
> [else (cons (first afile) (first-line (rest afile)))])]))
>
> ;; remove-first-line : file -> (listof symbol)
> ;; to compute the suffix of afile behind the first occurrence of NEWLINE
> (define (remove-first-line afile)
> (cond
> [(empty? afile) empty]
> [else (cond
> [(symbol=? (first afile) NEWLINE) (rest afile)]
> [else (remove-first-line (rest afile))])]))
>
> Because they appear structurally different, though, "first-line" and
> "remove-first-line" don't seem to me like good candidates for
> abstraction.
Hmmm...highlight the differences as the DR suggests. Structurally
these functions are very similar. You should discover two differences.
Once you highlight the differences, you will need to decide what the
extra parameters of the abstract function should be (i.e. the contract
of the abstract function). Keep in mind that if something needs to be
computed, then you need to pass-in a function (and it's OK for a
function to be a constant function).
Carl has given you good advice. Follow the steps of the DR.
--
Cheers,
Marco