<div>Dear reader,</div><div><br></div>M-x doctor suggested I consult with specialists as per aforementioned conditions, I pray this is the right list.<div><br></div><div>I'm having a very hard time with this exercise 12.4.2. I've faithfully followed the design recipes, but when it comes to the crunch, my lists aren't coming out right.</div>
<div><br></div><div>Please find below my exercise computer file, I've included all my data definitions, templates, examples, procedures and tears.</div><div><br></div><div><br></div><div><div>; A word is either</div><div>
; 1. empty; or</div><div>; 2. (cons a w) where a is a symbol ('a, 'b, ... 'z) and w is a word.</div><div><br></div><div>; (define (fun-for-word a-word)</div><div>; (cond</div><div>; [(empty? a-word) ...]</div>
<div>; [else ... (first a-word) ...</div><div>; ... (fun-for-word (rest a-word)) ...]))</div><div><br></div><div>; A list-of-words is either</div><div>; 1. empty; or</div><div>; 2. (cons w low) where w is a word and low is a list-of-words</div>
<div><br></div><div>; (define (fun-for-list-of-words a-low)</div><div>; (cond</div><div>; [(empty? a-low) ...]</div><div>; [else ... (first a-low) ...</div><div>; ... (fun-for-a-list-of-words (rest a-low)) ...]))</div>
<div><br></div><div>; EXAMPLES FOR DATA</div><div>; words;</div><div>; empty, </div><div>; 1-letter word: (cons 'a empty)</div><div>; 3-letter word: (cons 'a (cons 'p (cons 'e empty)))</div><div>; n-letter word: (cons x1 (cons x2 ( ... (cons xn-1 (cons xn empty)))</div>
<div>; low;</div><div>; 0 words: empty </div><div>; 1-word: (cons (cons 'a empty) empty)</div><div>; 3-words: (cons (cons (cons 'a empty)</div><div>; (cons (cons 'b empty)</div>
<div>; (cons (cons 'c empty)</div><div>; empty)))</div><div>; empty)</div><div><br></div><div><br></div><div>;; arrangements : word -> list-of-words</div>
<div>;; to create a list of all rearrangements of the letters in a-word</div><div>;; (I have a hard copy of the book, which gives a nice example for this)</div><div>(define (arrangements a-word)</div><div> (cond</div><div>
[(empty? a-word) empty]</div><div> [else (insert-everywhere/in-all-words (first a-word)</div><div> (arrangements (rest a-word)))]))</div><div><br></div><div>;; insert-everywhere/in-all-words : symbol list-of-words -> list-of-words</div>
<div>;; to insert s between and around all the letters of all the words of a-low</div><div>; EXAMPLES</div><div>; (insert-everywhere/in-all-words 'd</div><div>; (cons (cons 'e (cons 'r empty))</div>
<div>; (cons (cons 'r (cons 'e empty))</div><div>; empty)))</div><div>; ---></div><div>; (cons (cons 'd (cons 'e (cons 'r empty)))</div>
<div>; (cons (cons 'e (cons 'd (cons 'r empty)))</div><div>; (cons (cons 'e (cons 'r (cons 'd empty)))</div><div>; (cons (cons 'd (cons 'r (cons 'e empty)))</div>
<div>; (cons (cons 'r (cons 'd (cons 'e empty)))</div><div>; (cons (cons 'r (cons 'e (cond 'd empty)))</div><div>; empty))))))</div>
<div><br></div><div>(define (insert-everywhere/in-all-words s a-low)</div><div> (cond</div><div> [(empty? a-low) empty]</div><div> [else (append (insert-everywhere/in-one-word s (first a-low))</div><div> (insert-everywhere/in-all-words s (rest a-low)))]))</div>
<div><br></div><div>;; insert-everwhere/in-one-word symbol word -> list-of-words</div><div>;; to insert s between all letters and around a-word.</div><div>; EXAMPLES</div><div>; (insert-everywhere/in-one-word 'd (cons 'e (cons 'r empty)))</div>
<div>; ---></div><div>; (cons (cons 'd (cons 'e (cons 'r empty)))</div><div>; (cons (cons 'e (cons 'd (cons 'r empty)))</div><div>; (cons (cons 'e (cons 'r (cons 'd empty)))</div>
<div>; empty)))</div><div><br></div><div>(define (insert-everywhere/in-one-word s a-word)</div><div> (cond</div><div> [(empty? a-word) empty]</div><div> [else (append (insert-around-word s a-word)</div>
<div> (insert-between-word s a-word))]))</div><div><br></div><div>;; insert-around-word : symbol word -> list-of-words</div><div>;; to insert s on the front and back of a-word</div><div>; EXAMPLES</div><div>
; (insert-around-word 'd (cons 'e (cons 'r empty)))</div><div>; ---></div><div>; (cons (cons 'd (cons 'e (cons 'r empty)))</div><div>; (cons (cons 'e (cons 'r (cons d empty)))</div>
<div>; empty))</div><div><br></div><div>(define (insert-around-word s a-word)</div><div> (cons (cons s a-word)</div><div> (cons (append a-word (cons s empty))</div><div> empty)))</div><div>
<br></div><div>;; insert-between-word : symbol word -> list-of-words</div><div>;; to insert s inbetween all the adjacent letters</div><div>; EXAMPLES</div><div>; (insert-between-word 'd (cons 'e (cons 'r empty)))</div>
<div>; ---></div><div>; (cons (cons 'e (cons 'd (cons 'r empty)))</div><div>; empty)</div><div>; </div><div>; (insert-between-word 'd (cons 'a (cons 'p (cons 'e empty))))</div><div>; ---></div>
<div>; (cons (cons 'a (cons 'd (cons 'p (cons 'e empty))))</div><div>; (cons (cons 'a (cons 'p (cons 'd (cons 'e empty))))</div><div>; empty))</div><div><br></div><div>(define (insert-between-word s a-word)</div>
<div> (cond</div><div> [(empty? (rest a-word)) empty]</div><div> [else (cons (cons (first a-word) (cons s (rest a-word)))</div><div> (insert-between-word (cons (first (rest a-word))</div><div> (cons s empty))</div>
<div> (cons (first a-word) (rest (rest a-word)))))]))</div><div><br></div><div>If you got this far, it's this last definition above that's causing my symptoms. For example:</div>
<div><br></div><div><div>> (insert-between-word 'd (cons 'a (cons 'p (cons 'e empty))))</div><div>(cons (cons 'a (cons 'd (cons 'p (cons 'e empty)))) (cons (cons 'a (cons (cons 'p (cons 'd empty)) (cons 'e empty))) empty))</div>
<div><br></div><div>(I peaked a little ahead in the book, and found that I can write this output more succinctly like so:</div><div>((a d p e) (a (p d) e))</div><div>)</div><div><br></div><div>Thank you for reading all this. Any pointers on where I'm going wrong with the DR (I'm using the one on p132) would be much appreciated. </div>
<div><br></div><div>Yours Sincerely,</div><div><br></div><div>Horace.</div></div><div> </div></div>