[racket] style: nested cond (fine!)
On Feb 21, 2012, at 8:41 AM, Stephen Bloch wrote:
> On Feb 21, 2012, at 7:18 AM, Rodolfo Carvalho wrote:
>
>> BTW I just found the nested-cond pattern appearing on HtDP, throughout section 9:
>> http://htdp.org/2003-09-26/Book/curriculum-Z-H-13.html
>
> That happens when you follow the HtDP design recipe directly and there are conditionals for two unrelated reasons, e.g.
>
> (cond [(empty? L) ...]
> [(cons? L)
> (cond [(snark? (first L)) ...]
> [(boojum? (first L)) ...]
> [(beeblebrox? (first L)) ...]
> )])
>
> Yes, this COULD be collapsed into ...
We *subtract* points from solutions when students flatten out such conds in 'CS1'. At this stage in their development programmers must be able to go from a quasi-algebraic data definitions to code (and back). If they were to write this code in Java ('CS2'), they wouldn't have conds for the outer part -- they'd use dispatch. Hence I consider this version the only one that is correct -- morally speaking.
Adult/experienced programmers may flatten the cond -- if they know certain conditions hold about the data types involved and **all** programmers who will read their code in the future.
Eli knows all this -- which is why he wrote that the tool cannot be used without human post-processing. It isn't an automatic tool -- it must be placed in context.
-- Matthias