[plt-scheme] Re: HTDP Exercise 12.4.2 ... Help!

From: S Brown (ontheheap at gmail.com)
Date: Mon Apr 27 17:29:21 EDT 2009

What I ended up doing was creating a helper function to insert-
everywhere, which takes 3 arguments, the front of the word, the
symbol, and the back of the word. insert-everywhere doesn't do
anything except make the initial call to the insert-everywhere-helper
function, which does the actual work of inserting the letter into the
word. So, what I ended up with is basically this:

;; insert-everywhere:  symbol word  ->  list-of-words
;; creates a list of words from symbol s, and word w, where
;; s has been inserted into each position of w
(define (insert-everywhere s w) ... )

and also

;; insert-everywhere-helper: word symbol word  ->  list-of-words
;; creates a list of words where symbol is between word f and word b
(define (insert-everywhere-helper f s b) ... )

I don't know how correct this is, at least as far as what the book
wants, but it does pass the following check-expect calls:

  (check-expect (insert-everywhere 'x empty)
     (list (list 'x)))

  (check-expect (insert-everywhere 'x (list 'a 'b))
     (list (list 'x 'a 'b)
           (list 'a 'x 'b)
           (list 'a 'b 'x)))

  (check-expect (insert-everywhere 'x (list 'a 't 'o 'm))
     (list (list 'x 'a 't 'o 'm)
           (list 'a 'x 't 'o 'm)
           (list 'a 't 'x 'o 'm)
           (list 'a 't 'o 'x 'm)
           (list 'a 't 'o 'm 'x)))

  (check-expect (arrangements (list 'h 'o 't))
                (list (list 'h 'o 't)
                      (list 'o 'h 't)
                      (list 'o 't 'h)
                      (list 'h 't 'o)
                      (list 't 'h 'o)
                      (list 't 'o 'h)))

Thank you to everyone who provided input!

On Apr 26, 6:02 pm, Nadeem Abdul Hamid <nad... at acm.org> wrote:
> Consider having the purpose of your insert-everywhere function:
>
> > (insert-everywhere 'x (list 'h 'o 't))
>
> to be to produce this result by the time it's done:
>
> >    =>   (list (list 'x 'h 'o 't)
> >               (list 'h 'x 'o 't)
> >               (list 'h 'o 'x 't)
> >               (list 'h 'o 't 'x))
>
> and consider what function you wish you could use in the body of  
> insert-everywhere itself to achieve this (as opposed to thinking about  
> applying your helper function only after insert-everywhere has  
> produced the result that you currently have it producing).
>
> On Apr 26, 2009, at 5:37 PM, S Brown wrote:
>
>
>
> > I've been working on this for the last few hours, and my insert-
> > everywhere function is now working like so:
>
> > (insert-everywhere 'x (list 'h 'o 't))
>
> > =>   (list (list 'x 'h 'o 't)
> >           (list 'x 'o 't)
> >           (list 'x 't)
> >           (list 'x))
>
> > So, I wish I had some function that would prefix each list with the
> > "remainder" of the word, such that:
>
> >    (list (prefix remainder (list 'x 'h 'o 't))
> >          (prefix remainder (list 'x 'o 't))
> >          (prefix remainder (list 'x 't))
> >          (prefix remainder (list 'x)))
>
> >    =>   (list (list 'x 'h 'o 't)
> >               (list 'h 'x 'o 't)
> >               (list 'h 'o 'x 't)
> >               (list 'h 'o 't 'x))
>
> > So, based on this I want a function, prefix, which basically takes two
> > words, appends them, and returns the new list. So, I guess in order to
> > use this "prefix" function, I need to figure out how to define
> > "remainder," but now I seem to be stuck again because remainder
> > depends on information that isn't being passed along (ie, for (prefix
> > remainder (list 'x 't)), remainder needs to be (list 'h 'o), but I
> > can't figure out how to do this!
>
> > Am I at least on the right path with this? Any tips/hints??
>
> > On Apr 26, 1:25 pm, Matthias Felleisen <matth... at ccs.neu.edu> wrote:
> >> Yes, and don't forget, you may need *more* auxiliary functions than
> >> just insert-everywhere
>
> >> On Apr 26, 2009, at 1:19 PM, S Brown wrote:
>
> >>> Thanks for the input so far. I don't know how I didn't see this
> >>> before! Instead of calling append on (list s) (first a-low), what I
> >>> need is to call append on another function which takes as it's
> >>> arguments a symbol and a single word, which returns a list of words
> >>> where the symbol has been been inserted before and after each letter
> >>> in the word. In other words, where insert-everywhere/in-all-words is
> >>> concerned with a list of words, my new function is only concerned  
> >>> with
> >>> a single word.
>
> >>> So, now it's a matter of going through the design process for my new
> >>> function, which I'm going to call insert-everywhere.
>
> >>> ;; insert-everywhere: symbol word  ->  list-of-words
> >>> ;; to create a list-of-words where symbol s is inserted before
> >>> ;; and after each letter in the word w
> >>> (define (insert-everywhere s w)
> >>>    (cond
> >>>       ((empty? w) ... s ...)
> >>>       (else ... s (first w) ...
> >>>             ... (insert-everywhere s (rest w)) ...)))
>
> >>> I think this makes sense. Now it's a matter of completing each cond
> >>> line in my new function insert-everywhere.. and figuring out how to
> >>> actually insert a letter into each position of a word.
> >>> _________________________________________________
> >>>   For list-related administrative tasks:
> >>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> >> _________________________________________________
> >>   For list-related administrative tasks:
> >>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > _________________________________________________
> >  For list-related administrative tasks:
> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> _________________________________________________
>   For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.