All pastes #939613 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-03-12 13:13 UTC
rendered paste body
Due: Thurs, March 13
---

Bring solutions, on paper, to class.
---------------------------------


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)))))))


; ( define dupla
;    ( lambda ( a l )
;       ( cond ( ( null? l ) l )
;              ( else ( cons a
;                             ( dupla a ( cdr l ) ) )
;                     )
;              )
;       )
;    )
; 
; ( dupla 'a '( 1 2 3 ) )

          

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 ) '() )
             ( ( equal? ( car l ) a ) ( cons a ( cons a ( double 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 inarray2
   ( lambda ( needle haystack )
      ( cond ( ( null? haystack ) false )
             ( ( list? ( car haystack ) ) ( inarray2 needle ( car haystack ) ) )
             ( ( equal? needle ( car haystack ) ) true )
             ( else ( inarray2 needle ( cdr haystack ) ) )
             )
      )
   )








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 removeDuplicates
		  (lambda (L)