Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

HtDP Section 12.4: Rearranging Words


Hi all,

I'm going over How to Design Programs on my own and I got stuck on one
of the problems <http://htdp.org/2003-09-26/Book/curriculum-Z-
H-16.html#node_sec_12.4>:

----------

;; a word is:
;; 1. empty
;; 2. (cons s w)
;; where s is a symbol and w is a word

;; 12.4.1
;; a list-of-words is:
;; 1. empty
;; 2. (cons w low)
;; where w is a word and low is a list-of-words

;; 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 (insert-everywhere/in-all-words (car a-word)
                                          (arrangements (cdr a-
word)))]))

;; 12.4.2
;; insert-everywhere/in-all-words : symbol list-of-words -> list-of-
words
;; produces a list-of-words like its second argument, but with the
;; first argument inserted between all letters and at the beginning
and the and
;; of all words of the second argument
(define (insert-everywhere/in-all-words s a-low)
  (cond
    [(empty? a-low) empty]
    [else (cons (insert-everywhere s (car a-low))
                (insert-everywhere/in-all-words s (cdr a-low)))]))

(define (insert-everywhere s a-word)
  (cond
    [(empty? a-word) empty]
    [else (cons s
                (cons (car a-word)
                      (insert-everywhere s (cdr a-word))))]))

(define test (cons 't (cons 'e (cons 's (cons 't empty)))))
(define qux (cons 'q (cons 'u (cons 'x empty))))
(define mylow (cons test (cons qux empty)))

;(insert-everywhere 'a test) ; --> (a t a e a s a t a)
;(insert-everywhere/in-all-words 'a mylow) ; --> ((a t a e a s a t a)
(a q a u a x a))

;(insert-everywhere/in-all-words 'd mylow) ; --> ((d t d e d s d t) (d
q d u d x))

----------

Exercise 12.4.2 says "Develop the function insert-everywhere/in-all-
words. It consumes a symbol and a list of words. The result is a list
of words like its second argument, but with the first argument
inserted between all letters and at the beginning and the end of all
words of the second argument." Can I have a helper function like
insert-everywhere here?

Thanks,

-- Georgi

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc