; shw.s
; Scheme homework CS6371 Fall 1999

;1)
(define (drop n L)
	(cond ((null? L) nil)
              ((zero? n) L)  ;could also check (<= n 0)
	      (else (drop (- n 1) (cdr L)))))

;2)
(define (startswith L M)
	(cond ((null? L) #t)
	      ((null? M) #f)
	      ((equal? (car L) (car M)) (startswith (cdr L) (cdr M)))
	      (else #f)))

;3)
(define (last L)
	(cond ((null? L) nil)
	      ((null? (cdr L)) (car L))
              (else (last (cdr L)))))

;4)
(define (take n L)
        (cond ((zero? n) nil)  ;could also check (<= n 0) & (> n (length L))
	      ((null? L) nil)
	      (else (cons (car L) (take (- n 1) (cdr L))))))

;5)
(define (fromto m n L)     ; NO error checking!
	(drop (- m 1) (take n L)))

;6) p.602 #2: Structural Equality
(define (sequal L M)
	(cond ((null? L) (null? M))
	      ((null? M) nil)
              ((atom? L) (atom? M))
              ((atom? M) nil)
              ((sequal (car L) (car M)) (sequal (cdr L) (cdr M)))
              (else nil)))

;7) p.602 #3: Union
(define (mymember x L)
	(cond ((null? L) nil)
	      ((eq? x (car L)) #t)
              (else (mymember x (cdr L)))))
(define (union L M)
	(cond ((null? L) M)
              ((mymember (car L) M) (union (cdr L) M))
	      (else (cons (car L) (union (cdr L) M)))))

;8) p.602 #4: Remove all
(define (removeall x L)
	(cond ((null? L) nil)
	      ((atom? (car L)) (if 
			       (eq? x (car L)) 
			       (removeall x (cdr L))
                               (cons (car L) (removeall x (cdr L)))))
              (else (cons (removeall x (car L)) (removeall x (cdr L))))))


