(require htdp/testing)

; takes a symbol named item and a list
; and checks for membership of item in lst
(define (my-memq item lst)
  (cond ((null? lst) #f)
        ((eq? item (car lst))
         lst)
        (else (my-memq item
                    (cdr lst)))))

(check-expect (my-memq 'a '(a b c)) '(a b c))
(check-expect (my-memq 'd '(a b c)) #f)
(check-expect (my-memq '(a b) '((a b) (a b) c)) #f)

; counts the number of times symbol item
; occurs in top level of lst
(define (item-count item lst)
  (define (item-count-iter sublist count)
    (let ((next-sublist (my-memq item sublist)))
      (if (not next-sublist)
          count
          (item-count-iter (cdr next-sublist)
                           (+ count 1)))))
  (item-count-iter lst 0))

(check-expect (item-count 'a '(a b c a a a b a)) 5)
(check-expect (item-count 'a '(b c d e)) 0)

; takes two expressions and is #t if they are the 
; same symbols, or if they are lists with same elements
; and structure -- i.e., they look the same
(define (my-equal? s1 s2)
  (cond ((symbol? s1)
         (eq? s1 s2))
        ((symbol? s2) #f)
        ((and (null? s1) (null? s2))
         #t)
        ((or (null? s1) (null? s2))
         #f)
        (else
         (and (my-equal? (car s1) (car s2))
              (my-equal? (cdr s1) (cdr s2))))))

(check-expect (my-equal? 'a 'a) #t)
(check-expect (eq? 'a 'a) #t)
(check-expect (my-equal? '(a) '(a)) #t)
(check-expect (eq? '(a) '(a)) #f)

(generate-report)