;;; Michael Wollowski ;;; A5 (define (rl) (load "5.ss")) (define free-vars (case-lambda [(exp) (free-vars exp '() '())] [(exp free bound) (cond [(null? exp) free] [(symbol? exp) (if (memq exp bound) free (merge exp free))] [(eq? (car exp) 'lambda) (free-vars (cddr exp) free (merge (cadr exp) bound))] [(null? (cdr exp)) (free-vars (car exp) free bound)] [else (merge (free-vars (car exp) free bound) (free-vars (cadr exp) free bound))])])) (define merge (lambda (x y) (cond [(null? x) y] [(symbol? x) (if (memq x y) y (cons x y))] [else (merge (car x) (merge (cdr x) y))]))) (define bound-vars (case-lambda [(exp) (bound-vars exp '())] [(exp bound) (cond [(null? exp) ()] [(symbol? exp) (if (memq exp bound) (list exp) ())] [(eq? (car exp) 'lambda) (bound-vars (cddr exp) (append (cadr exp) bound))] [(null? (cdr exp)) (bound-vars (car exp) bound)] [else (append (bound-vars (car exp) bound) (bound-vars (cadr exp) bound))])])) (define gr '((a (b c)) (b (a)) (c (a)) (d ()))) (define connected? (lambda (g) (cond [(null? g)] [(and (null? (cdr g)) (null? (cdar g)))] [else (let* ([nodes (map car g)] [startEndPoints (make_start_end_points nodes)]) (paths? startEndPoints g))]))) (define make_start_end_points (lambda (l) (if (null? (cdr l)) () (append (map (lambda (x) (list (car l) x)) (cdr l)) (make_start_end_points (cdr l)))))) (define paths? (lambda (SEPoints g) (if (null? SEPoints) #t (and (path? (cadar SEPoints) g (caar SEPoints)) (paths? (cdr SEPoints) g))))) ;(define path? ; (lambda (endPoint g openqueue closedqueue ) ; (cond [(null? queue) #f] ; [(eq? (car queue) endPoint) #t] ; [else (path? endPoint g (append (cdr queue) ;;; remove duplicates from open and closed ;(get (car queue) g)))]))) (define get (lambda (e g) (cond [(null? g) ()] [(eq? e (caar g)) (cadar g)] [else (get e (cdr g))]))) (define remove-duplicates (lambda (l1 l2) (if (null? l1) l2 (remove-duplicates (cdr l1) (remq (car l1) l2)))))