[plt-scheme] HTDP 12.4.2

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Wed Jun 21 21:24:08 EDT 2006

> This is an excercise that I skipped as being too difficult the first 
> time I attempted HTDP.
> http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-16.html#node_sec_12.4
> I was on the verge of giving up again when I managed to write an 
> insertEverywhere that (it appears to me at least) works to required 
> specification.

Hi Wooks Wooks,

Looking through the use of the design patterns here, I see some slight 
issues.  Some are pedantic, but others do need some thorught.

Let's look at the pedantic problem first.  *grin*

> ;insertEverywhere : symbol ('a - 'z) list (word) - list of lists
> ;returns a list of words constructed by interpersing symbol in  position of 
> the word

What's a "word"?  Also, can you be more specific by "list of lists"? 
What does that mean?  Would ((1 2) (3 4)) be a possible return value for 
this function?

Although it might seem pedantic at first, aActually, concentrating on the 
contract here will be useful.  It's not just for busywork: it's to detect 
problems with code, as we'll see below.

> (insertEverywhere 'g (cons 'a (cons 'b (cons 'c empty))))

There's something missing here.  What should we expect to see from 
insertEverywhere in this particular example?  A test isn't just one of 
usage: it's one of expectations.

> ;infiltrate : symbol list list - list of words
> ; (define (infiltrate infiltrator front back) ...
> ; auxiliary function for insertEverywhere

What's the purpose of infiltrate?  All that is being said here is that it 
helps insertEverywhere, but other than that, no clue what it's use for 
besides espionage.  Also, other components of the design pattern template 
are missing: can you think of test cases to see if infiltrate is doing the 
right thing?

> ;; arrangements : word  ->  list-of-words
> ;; to create a list of all rearrangements of the letters in a-word
> (define (arrangements a-word)
> (cond
>   [(empty? a-word) (cons empty empty)]
>   [else (insertEverywhere (first a-word)
>           (arrangements (rest a-word)))]))
> (arrangements  (cons 'a (cons 'b (cons 'c empty))))

Again, what's the expected value of the test here?

On a closer look: there is definitely some confusion here, and if we look 
closer at the contracts here, we should be able to detect the problem even 
without running this code.

Look at the contracts to insertEverywhere once you've cleaned them up, and 
then look at how it's being used in arrangements.  You should see 
something funky.

Good luck to you.

Posted on the users mailing list.