Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; variable initialization
- ( define B '( d e ) )
- ( define A '( a b c d e ) )
- ( define C '( z v f e ) )
- ( define D '( ) )
- ( define T
- '(13
- (5
- (1 () ())
- (8 ()
- (9 () ())))
- (22
- (17 () ())
- (25 () ()))
- ))
- (define t
- '(13
- () ;(5 () ())
- (22 () () )
- )
- )
- (define LIST_3 '(1 2 3 (4 3) 5 (6 (3 7 )) 8 ) )
- (define ( is-set? L)
- ( cond
- ( ( null? L ) #t)
- ( (is-InList? (car L) (cdr L) ) #f)
- ( else ( is-set? (cdr L) ) ) ) )
- (define ( is-InList? elementInTheList ActualList )
- ( cond
- ( ( null? ActualList ) #f ) ; base case
- ( (equal? elementInTheList ( car ActualList)) #t ) ;
- ( else ( is-InList? elementInTheList (cdr ActualList)) )
- )
- )
- ; write a recursive function ( make-set L ) which returns a set built from list L by removing duplicates, if any.
- ; if we have reached the end of the list, return the empty list
- ; if the current element in the list is equal to the next element in the list, then we need to remove either one of the equal elements from the list and keep running the function on the list until there are no more repeated elements
- ;
- ( define ( remove-duplicates? item _List )
- (
- cond
- ;; test case 1
- ( ( null? _List ) '() )
- ;; test case 2
- ; ( ( is-InList? ( car _List ) ( cdr _List ) ) ( remove-duplicates? _List ) )
- ( ( equal? item (car _List) ) (append '() ( remove-duplicates? item (cdr _List )) ) )
- (else ( append ( (car L) (remove-duplicates? item (cdr _List)) ) ))
- )
- )
- (define (make-set _List)
- (
- cond
- ( (null? _List) '() )
- ( (is-InList? (car _List)(cdr _List)) (make-set ( cdr _List ) ) )
- ( else ( cons ( car _List) (make-set(cdr _List))))
- )
- )
- (define ( subset? A S )
- (
- cond
- ( (null? A) #t )
- ( ( not ( is-InList? ( car A ) S ) ) #f )
- ( else ( subset? (cdr A) S ) )
- )
- )
- (define ( intersection A B )
- (
- cond
- ( ( or (null? A) (null? B) ) '() ); return empty list
- ( ( is-InList? ( car A ) B ) (cons ( car A ) (intersection (cdr A) B ) ) )
- ( else ( append '() ( intersection (cdr A) B ) ) )
- )
- )
- (define (union A B)
- (make-set (append A B) )
- )
- ; BST auxilary functions
- (define ( val T ) ( car T ) )
- (define (left T) (cadr T) )
- (define (right T) (caddr T) )
- ( define (tree-member? value Tree)
- (
- cond
- ( (null? Tree ) #f )
- ( (equal? value (val Tree)) #t )
- ( ( > value ( val Tree ) ) (tree-member? value (right Tree) ) )
- ( else ( tree-member? value (left Tree) ) )
- )
- )
- (define (preorder? Tree)
- (
- cond
- ( ( null? Tree ) '() )
- (
- append
- ( list (val Tree) )
- ( list (left Tree) )
- ( list (right Tree) )
- )
- )
- )
- (define (inorder T)
- (cond ((null? T) '())
- (else (append (inorder (left T))
- (list (val T))
- (inorder (right T))))))
- (define (deep-delete value _List)
- (
- cond
- ( (null? _List) '() )
- ( (list? (car _List)) (append (list(deep-delete value (car _List) )) (deep-delete value (cdr _List)) ) )
- (else (cons (car _List) (deep-delete value (cdr _List))))
- )
- )
- (define (insert-bst V T)
- (cond ((null? T)(cons V '()))
- ((< V (val T)) (list (val T)
- (insert-bst V (left T))
- (right T)))
- ((> V (val T)) (list (val T)
- (left T)
- (insert-bst V (right T))))
- (else T)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement