Tuesday, 15 May 2012

Exercise 167: Write down the data definition for List-of-words.

Systematically make up examples of Words and List-of-words. Finally, formulate the functional example from above with check-expect.  Instead of working with the full example, you may wish to start with a word of just two letters, say "d" and "e".
 A Word is either
 – empty or
 – (cons 1String Word)

I think this is the correct data definition for a List-of-words. It is either an empty list, or a Word cons'd together with another List-of-words (which may itself be an empty word)

; A List-of-words is either
; - empty or
; (cons Word List-of-words)



For our examples we will create two Words - "word" and "roger".  There is a 1String function we could use to do this but for practice I'm using cons directly.

(define word (cons "w" (cons "o" (cons "r" (cons "d" empty)))))  
(define roger (cons "r" (cons "o" (cons "g" (cons "e" (cons "r" empty))))))


Now an example List-of-words containing these two words:
(define list-of-words (cons word (cons roger empty))) 


For testing this example we will create a 1String for the string "de"
(define de (cons "d" (cons "e" empty)))


Now a check-expect to test all arrangements of the word "de".
This will be failing at this time as we haven't implemented the arrangements function yet.
(check-expect (arrangements de)
              (cons (cons (cons "d" (cons "e" empty))
                    (cons "e" (cons "d" empty)))
                     empty))

No comments:

Post a Comment