I should have been more explicit: "ensuring the function design conforms to its contract" means testing it in the manner demonstrated by Matthias. "Check-expect" is your friend :). On Jul 1, 2012, at 4:12 PM, Matthias Felleisen wrote:
> > On Jul 1, 2012, at 4:17 PM, Sean Kemplay wrote: > >> Looking back, I am wondering if my solution is a bit of a cheat - >> specifically : >> >> (define (insert-everwhere/in-one-word s word) >> (make-words s word word)) > > > It's not a cheat, it's wrong. I did you a favor and worked out a failing test > case. See below. > > > On Jul 1, 2012, at 6:46 PM, Dave Yrueta wrote: > >> Try working through this exercise by systematically applying the design >> recipe. That is, take the time to make your data definitions explicit, and >> begin the definition of each helper function with a contract, purpose >> statement, and template. In my opinion, more than any other exercise in >> HtDP, the solution to this exercise relies on paying very close attention to >> how the data definitions, function contracts and function templates >> interact. If you allow the data definitions to shape the function templates, >> and are rigorous about ensuring the function designs conform to their >> contracts, the solution should fall into place. > > > The above is the best advice the list can give you. -- Matthias > > > > > ;; World = [Listof Letter] ;; see chapter IV > ;; Letter = Symbol > > ;; Letter Word -> [Listof Word] > ;; the function says: insert _s_ in all positions in _word_ > > (check-expect (insert-everwhere/in-one-word 'a '(w o)) > '((a w o) (w a o) (w o a))) > (check-expect (insert-everwhere/in-one-word 'a '(w w o)) > '((a w w o) (w a w o) (w w a o) (w w o a))) > > (define (insert-everwhere/in-one-word s word) > (make-words s word word)) > > ;; Letter Word Word -> [Listof Word] > ;; insert _s_ in all positions in _word2_ using _word1_ as a list of > insertion points > ;; intended usage: (make-words letter word word) i.e. word1 = word2 initially > > (check-expect (make-words 'a '(w o) '(w o)) '((a w o) (w a o) (w o a))) > (check-expect (make-words 'a '(w w o) '(w w o)) '((a w w o) (w a w o) (w w a > o) (w w o a))) > > (define (make-words s word1 word2) > (cond > [(empty? word1) (cons (append word2 (cons s empty)) empty)] > [else (cons (insert-symbol s (first word1) word2) (make-words s (cdr word1) > word2))])) > > > ;; Letter Letter Word -> Word > ;; add _new_ in front of each occurrence of _old_ in _word_ > > (check-expect (insert-symbol 'a 'b '(c d e)) '(c d e)) ;; running in BSL with > .. > (check-expect (insert-symbol 'a 'b '(b o b)) '(a b o a b)) > > (define (insert-symbol new old word) > (cond > [(empty? word) empty] > [(symbol=? (first word) old) (cons new (cons old (insert-symbol new old > (rest word))))] > [else (cons (first word) (insert-symbol new old (rest word)))])) > > > ____________________ > Racket Users list: > http://lists.racket-lang.org/users ____________________ Racket Users list: http://lists.racket-lang.org/users