;1. Write the procedure (dupla n L) that creates a list of
; by replacing each data expression of L with n.
; ex. (dupla 'a '(1 2 3)) -> '(a a a)
; (dupla 'a '()) -> '()
; (dupla '(1 2) '(a b c)) -> '( (1 2) (1 2) (1 2))
(define dupla
(lambda (n L)
(cond ((null? L) L)
(else (cons n
(dupla n (cdr L)))))))
;2. Complete the function double that inputs a data expression a and a list
; L and doubles the first occurrence of the data expression a in the list
; L.
; ex. (double 'cat '(dog cat rat)) -> '(dog cat cat rat)
(define double
(lambda (a L)
(cond ((null? L) L)
((equal? a (car L)) (cons a (cons a (cdr L))))
(else (cons (car L) (double a (cdr L)))))))
;3. Write the procedure (atom-occur? n L) that returns #t if atom n
; occurs anywhere within list L; otherwise #f.
; ex. (atom-occur? 'a '( )) -> #f
; (atom-occur? 'a '( d a d )) -> #t
; (atom-occur? 2 '( 1 (((2) 3)))) -> #t
; (atom-occur? 5 '( (1) (2) 3 )) -> #f
; (atom-occur? 'a '( () a)) -> #t
(define atom-occur?
(lambda (a s)
(cond ((null? a) (null? s))
((null? s) #f)
((equal? a (car s)) #t)
((list? (car s)) (or (atom-occur? a (car s))
(atom-occur? a (cdr s))))
(else (atom-occur? a (cdr s))))))
;4. Write the function (removeDuplicates L) where L is a list. This function
; should remove all the duplicate data expressions in list L. You may use the
; (mymember? n L) that you wrote for homework.
; ex. (removeDuplicates '(a b a c)) -> '(b a c)
; (removeDuplicates '((a b) (a b) d)) -> '((a b) d)
; (removeDuplicates '(1 2 3 4 5 )) -> '(1 2 3 4 5)
(define myMember?
(lambda (x L)
(cond ((and (null? x) (null? L)) #t)
((null? L) #f)
((equal? x (car L)) #t)
(else (myMember? x (cdr L))))))
(define removeDuplicates
(lambda (L)
(cond ((null? L) L)
((atom-occur? (car L) (cdr L))
(cons (cadr L)
(removeDuplicates (cddr L))))
(else (cons (car L) (removeDuplicates (cdr L)))))))