[plt-scheme] Re: Permutation correct way to do it ?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Apr 5 18:31:13 EDT 2008

>

Just a few comments left:

> (define (insert-at-begining a-sym a-word)
>   (cond
>     [(empty? a-word) (cons a-sym empty)]
>     [else (cons a-sym a-word) ]))

True or false: no matter what a-word is (empty? or cons?) the answer  
is built the same way?


> ;;to insert a-sym at the end of a-word
> ;;Example : given  'a (cons 'b (cons 'c empty)) -> (cons 'b (cons  
> 'c (cons 'a empty)))
> (define (insert-at-end a-sym a-word)
>   (cond
>     [(empty? a-word) (cons a-sym empty) ]
>     [else (append a-word (cons a-sym empty)) ]))

True or false: no matter what a-word is (empty? or cons?) the answer  
is built the same way?

Simplify these functions.

;; ---

Let me recommend that you read chapter iv up to page 313 (and end of  
that section) next.
Then "edit" the above program using the "list abstractions" (map,  
fold, etc) from
chapter iv. Replace one function at a time, make sure the tests pass  
before you move on.
Enjoy the test cases while you're at it and even more the powerful  
result.

-- Matthias



Posted on the users mailing list.