views:

917

answers:

6

how to design a function content which inputs a single list of atoms lat and which returns the content of lat.Thus the content of '(a b c a b c d d) is '(a b c d).

+2  A: 

The procedure content below should get you what you need.

(define (work x y)
  (if (null? (cdr x))
      (if (in? (car x) y)
          y 
          (cons (car x) y))
      (if (in? (car x) y)
          (work (cdr x) y)
          (work (cdr x) (cons (car x) y)))))

(define (in? x y)
  (if (null? y)
      #f
      (if (equal? x (car y))
          #t
          (in? x (cdr y)))))

(define (content x) (work x (list)))

The procedure content accepts a list as a parameter. It sends the list to another procedure called work. This procedure processes the list and adds the items in the list to a new list (if they are not already in the new list). The work procedure makes use of yet another procedure called in, which checks to see if an item is a member of a list.

My solution essentially divides your problem into two sub-problems and makes use of procedures which operate at a lower level of abstraction than your original problem.

Hope that helps.

ecounysis
@Eric - honestly, that code formatting is the most unlike Lisp you can get without adding begin/end keywords. See http://stackoverflow.com/questions/541407/lisp-code-formatting, for instance
dsm
I imagine that the reason he seemed so hostile was because you were providing this code to a person asking for homework. I suppose his teacher probably expects proper formatting.
Rayne
that's the point ... "proper" to you may not be "proper" to me
ecounysis
+1  A: 

It is PLT Scheme solution:

(define (is_exists list element)
 (cond
   [(empty? list) false]
   [else
     (cond 
       [(= (first list) element) true]
       [else (is_exists (rest list) element)])]))

(define (unique list target)
  (cond 
    [(empty? list) target]
    [else
     (cond 
       [(is_exists target (first list)) (unique (rest list) target)]
       [else (unique (rest list) (cons (first list) target))])]))

(define (create_unique list)
        (unique list empty))

Check it:

> (define my_list (cons '1 (cons '2 (cons '3 (cons '2 (cons '1 empty))))))
> my_list
(list 1 2 3 2 1)
> (create_unique my_list)
(list 3 2 1)
demas
Isn't the whole point of `cond` that you don't need to chain else-clauses? :-)
Ken
A: 

Start from a procedure that simply creates a copy of the passed-in list (very easy to do):

(define (unique-elements seq)
  (define (loop ans rest)
    (cond ((null? rest) ans)
          (else
           (loop (cons (car rest) ans)
                 (cdr rest)))))
  (loop '() seq))

To ensure that the output list's elements are unique, we should skip the CONS if the head of REST is already a member of ANS. So we add another condition to do just that:

;;; Create list containing elements of SEQ, discarding duplicates.
(define (unique-elements seq)
  (define (loop ans rest)
    (cond ((null? rest) ans)
          ((member (car rest) ans)  ; *new*
           (loop ans (cdr rest)))   ; *new*
          (else
           (loop (cons (car rest) ans)
                 (cdr rest)))))
  (loop '() seq))
Cirno de Bergerac
A: 

The following function takes in a list and returns a new list with only the unique inputs of it's argument using recursion:

(defun uniq (list)
    (labels ((next (lst new)
                (if (null lst)
                    new
                    (if (member (car lst) new)
                        (next   (cdr lst) new)
                        (next   (cdr lst) (cons (car lst) new))))))
        (next list ())))

As was mentioned in the comments, common lisp already has this function:

(defun uniq (list)
    (remove-duplicates list))
dsm
(defun uniq (list) (remove-duplicates list))
Rainer Joswig
But where is the fun in that? ;-)
dsm
why the -1 vote?
dsm
A: 
(define (remove-duplicates aloc)
  (cond
    ((empty? aloc) '())
    (else (cons (first aloc)
                (remove-duplicates
                 (filter (lambda (x)
                           (cond
                             ((eq? x (first aloc)) #f)
                             (else #t)))
                         (rest aloc)))))))
philip
A: 

How about little schemer style,

(define (rember-all a lat)
  (cond
    ((null? lat) '())
    ((eq? a (car lat)) (rember-all a (cdr lat)))
    (else (cons (car lat) (rember-all a (cdr lat))))))

(define (content lat)
  (cond
    ((null? lat) '())
    (else (cons (car lat)
                (content (rember-all (car lat) (cdr lat)))))))
Lewis Jubb