Advertisement
Guest User

HWK3

a guest
Sep 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.74 KB | None | 0 0
  1. ;;; variable initialization
  2. ( define B '( d e ) )
  3. ( define A '( a b c d e ) )
  4. ( define C '( z v f e ) )
  5. ( define D '(  ) )
  6. ( define T
  7.     '(13  
  8.         (5  
  9.             (1 () ())  
  10.             (8 ()  
  11.                 (9 () ())))  
  12.         (22  
  13.             (17 () ())  
  14.             (25 () ()))
  15. ))
  16.  
  17. (define t
  18.     '(13
  19.             ()  ;(5 () ())
  20.                
  21.                 (22 () () )
  22.     )
  23. )
  24.  
  25. (define LIST_3 '(1 2 3 (4 3) 5 (6 (3 7 )) 8 ) )
  26.      
  27.  
  28.  
  29. (define ( is-set? L)
  30.     ( cond
  31.       ( ( null? L ) #t)
  32.       ( (is-InList? (car L) (cdr L) ) #f)
  33.       ( else ( is-set? (cdr L) ) ) ) )
  34.      
  35.  
  36. (define ( is-InList? elementInTheList ActualList )
  37.     ( cond
  38.       ( ( null? ActualList ) #f ) ; base case
  39.       ( (equal? elementInTheList ( car ActualList)) #t ) ;
  40.       ( else ( is-InList? elementInTheList (cdr ActualList)) )
  41.     )
  42. )
  43.      
  44. ; write a recursive function ( make-set L ) which returns a set built from list L by removing duplicates, if any.
  45.  
  46. ; if we have reached the end of the list, return the empty list
  47.  
  48. ; 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
  49.  
  50. ;
  51.  
  52.  
  53.  
  54.  
  55. ( define ( remove-duplicates? item _List )
  56.     (
  57.         cond
  58.        
  59.         ;; test case 1
  60.         ( ( null? _List ) '() )
  61.        
  62.         ;; test case 2
  63.         ; ( ( is-InList? ( car _List ) ( cdr _List ) ) ( remove-duplicates? _List ) )
  64.         ( ( equal? item (car _List) ) (append '() ( remove-duplicates? item (cdr _List )) )  )
  65.        
  66.         (else ( append ( (car L) (remove-duplicates? item (cdr _List)) ) ))
  67.        
  68.     )
  69. )
  70.  
  71. (define (make-set _List)
  72.     (
  73.         cond
  74.        
  75.         ( (null? _List) '() )
  76.        
  77.         ( (is-InList? (car _List)(cdr _List)) (make-set ( cdr _List ) ) )
  78.         ( else ( cons ( car _List) (make-set(cdr _List))))
  79.     )
  80. )
  81.  
  82.  
  83. (define ( subset? A S )
  84.     (
  85.       cond
  86.      
  87.       ( (null? A) #t )
  88.      
  89.       ( ( not ( is-InList? ( car A ) S ) ) #f )
  90.      
  91.       ( else ( subset? (cdr A) S ) )
  92.    
  93.     )
  94. )
  95.  
  96.  
  97. (define ( intersection A B )
  98.     (
  99.         cond
  100.             ( ( or (null? A) (null? B) ) '() ); return empty list
  101.             ( ( is-InList? ( car A ) B ) (cons ( car A ) (intersection (cdr A) B ) ) )
  102.             ( else ( append '() ( intersection (cdr A) B ) ) )
  103.     )
  104. )
  105.  
  106. (define (union A B)
  107.    
  108.         (make-set (append A B) )
  109. )
  110.  
  111. ; BST auxilary functions
  112. (define ( val T ) ( car T ) )
  113. (define (left T) (cadr T) )
  114. (define (right T) (caddr T) )
  115.  
  116.  
  117. ( define (tree-member? value Tree)
  118.     (
  119.         cond
  120.         ( (null? Tree ) #f  )
  121.         ( (equal? value (val Tree)) #t )
  122.         ( ( > value ( val Tree ) ) (tree-member? value (right Tree)  ) )
  123.         ( else ( tree-member? value (left Tree) ) )
  124.     )
  125. )
  126.        
  127.  
  128. (define (preorder? Tree)
  129.  
  130.     (
  131.         cond
  132.            
  133.         ( ( null? Tree ) '() )
  134.        
  135.         (
  136.             append
  137.            
  138.                 ( list (val Tree) )
  139.                 ( list (left Tree) )
  140.                 ( list (right Tree) )
  141.                
  142.          )
  143.     )
  144. )
  145.  
  146.  
  147. (define (inorder T)
  148.     (cond ((null? T) '())
  149.          (else (append (inorder (left T))
  150.                        (list (val T))
  151.                        (inorder (right T))))))
  152.  
  153.      
  154.      
  155. (define (deep-delete value _List)
  156.     (
  157.       cond
  158.      
  159.       ( (null? _List) '() )
  160.      
  161.       ( (list? (car _List)) (append (list(deep-delete value (car _List) )) (deep-delete value (cdr _List)) )  )
  162.      
  163.       (else (cons (car _List) (deep-delete value (cdr _List))))
  164.     )
  165. )
  166.    
  167.    
  168. (define (insert-bst V T)
  169.     (cond ((null? T)(cons V '()))
  170.          ((< V (val T)) (list (val T)
  171.                               (insert-bst V (left T))
  172.                               (right T)))
  173.           ((> V (val T)) (list (val T)
  174.                                (left T)
  175.                                (insert-bst V (right T))))
  176.           (else T)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement