For Programmers: Free Programming Magazines  


Home > Archive > Scheme > June 2007 > HtDP Section 12.4: Rearranging Words









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author HtDP Section 12.4: Rearranging Words
hWnd

2007-06-01, 10:13 pm

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

Brocks

2007-06-05, 2:40 pm

Hilary Swank and Jessica Alba ,Insert Bottly In XXXXXXX!
http://www.YourTubeAmp.com/WatchTube?watch=1673286
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com