<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Aug 29, 2014, at 4:37 PM, Daniel Bastos wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: 'Lucida Grande'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">Perhaps I didn't understand this exercise. The hint advises me to work with a particular case and then generalize. I did this, but it seems that even in a particular case the most natural solution seems to be the general one. The generalization took place only in the data definition, but it didn't seem to affect the function definition.</span></blockquote></div><br><div><br></div><div>Correct. The most appropriate solution is going to be: </div><div><br></div><div><div>;; Exercise 19.2.4. </div><div><br></div><div>;; Draft. a (non-empty-listof number) is either</div><div>;; 1. (cons s empty), or</div><div>;; 2. (cons s l) where l is a (non-empty-listof number)</div><div>;; and s is always a number.</div><div><br></div><div>;; last-number: non-empty-ls -> number</div><div>;; consumes a non-empty list of numbers and produces its last number</div><div><br></div><div>(check-expect (last-number (list 1 2 3 4 5)) 5)</div><div>(check-expect (last (list (list 1) (list 2) (list 3))) (list 3))</div><div><br></div><div>(define (last-number non-empty-ls)</div><div>  (cond</div><div>    [(empty? (rest non-empty-ls)) (first non-empty-ls)]</div><div>    [else (last-number (rest non-empty-ls))]))</div><div><br></div><div>;; Generalizing to ITEM.</div><div><br></div><div>;; a (non-empty-listof ITEM) is either</div><div>;; 1. (cons s empty), or</div><div>;; 2. (cons s l) where l is a (non-empty-listof ITEM)</div><div>;; and s is always an ITEM.</div><div><br></div><div>;; last: (non-empty-listof ITEM) -> ITEM</div><div>;; consumes a non-empty list of ITEMs and produces its last ITEM</div><div><br></div><div>(check-expect (last '(a b c d e f)) 'f)</div><div><br></div><div>(define (last non-empty-ls)</div><div>  (cond</div><div>    [(empty? (rest non-empty-ls)) (first non-empty-ls)]</div><div>    [else (last (rest non-empty-ls))]))</div><div><br></div><div>;; INSIGHT: in this case, the data-specific function and the </div><div>;; data-parametric one are identical because the former does </div><div>;; not exploit the knowledge about the nature of the list items.</div><div><br></div></div></body></html>