Advertisement
Guest User

Untitled

a guest
Jun 8th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.40 KB | None | 0 0
  1. #lang racket
  2.  
  3. ; Signature: make-tree(1st, ..., nth)
  4. ; Type: [Tree * ... * Tree -> Tree]
  5. (define make-tree list)
  6. ; Signature: add-subtree(subtree, tree)
  7. ; Type: [Tree * Tree -> Tree]
  8. (define add-subtree cons)
  9. ; Signature: make-leaf(data)
  10. ; Type: [T -> Tree]
  11. (define make-leaf (lambda (d) d))
  12. ; Signature: empty-tree
  13. ; Type: Empty-Tree
  14. (define empty-tree empty)
  15. ; Signature: first-subtree(tree)
  16. ; Type: [Tree -> Tree]
  17. (define first-subtree car)
  18. ; Signature: rest-subtrees(tree)
  19. ; Type: [Tree -> Tree]
  20. (define rest-subtrees cdr)
  21. ; Signature: leaf-data(leaf)
  22. ; Type: [Tree -> T]
  23. (define leaf-data (lambda (x) x))
  24. ; Signature: composite-tree?(e)
  25. ; Type: [T -> Boolean]
  26. (define composite-tree? pair?)
  27. ; Signature: leaf?(e)
  28. ; Type: [T -> Boolean]
  29. (define leaf? (lambda (t) (not (list? t))))
  30. ; Signature: empty-tree?(e)
  31. ; Type: [T -> Boolean]
  32. (define empty-tree? empty?)
  33.  
  34. ;; The empty lazy list value (a singleton datatype)
  35. (define empty-lzl '())
  36.  
  37. ;; Purpose: Value constructor for non-empty lazy-list values
  38. ;; Type: [T * [Empty -> LZL(T)] -> LZT(T)]
  39. (define cons-lzl cons)
  40.  
  41. ;; Accessors
  42. ;; Type: [LZL(T) -> T]
  43. ;; Precondition: Input is non-empty
  44. (define head car)
  45.  
  46. ;; Type: [LZL(T) -> LZL(T)]
  47. ;; Precondition: Input is non-empty
  48. ;; Note that this *executes* the continuation
  49. (define tail
  50.   (lambda (lzl)
  51.   ((cdr lzl))))
  52.  
  53. ;; Type predicate
  54. (define empty-lzl? empty?)
  55.  
  56. ;; Signature: take(lz-lst,n)
  57. ;; Type: [LzL*Number -> List]
  58. ;; If n > length(lz-lst) then the result is lz-lst as a List
  59. (define take
  60.   (lambda (lz-lst n)
  61.     (if (or (= n 0) (empty-lzl? lz-lst))
  62.       empty-lzl
  63.       (cons (head lz-lst)
  64.             (take (tail lz-lst) (- n 1))))))
  65.  
  66. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  67. ;;            start            ;;
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69.  
  70. ; Signature: tree->leaves(tree : Tree)
  71. ; Type: [Tree -> List(Any)]
  72. ; Purpose: Returns an ordered list of the labels which appear in the leaves of the tree.
  73. ; Precondition: tree is of type Tree.
  74. ; Test: (tree->leaves '(a (b c)(((x y))))) => '(a b c x y)
  75. (define tree->leaves
  76.   (lambda (tree)
  77.     (if (empty-tree? tree) '()
  78.         (if (leaf? tree) (list tree)
  79.             (append (tree->leaves (first-subtree tree)) (tree->leaves (rest-subtrees tree)))
  80.         )
  81.     )
  82.  ))
  83.  
  84. ; Signature: list-to-lzlist(lst : List)
  85. ; Type: [List(Any) -> LZL]
  86. ; Purpose: Returns a lazy list of a list
  87. ; Precondition: lst is a list
  88. ; Test: (take (list-to-lzlist '(1 2 3)) 2) => '(1 2)
  89. (define list-to-lzlist
  90.   (lambda (lst)
  91.     (cons-lzl (car lst)
  92.               (lambda ()
  93.                 (if (empty? (cdr lst))
  94.                     empty-lzl
  95.                     (list-to-lzlist (cdr lst))        
  96.                 )
  97.     )
  98. )))
  99.  
  100. ; Signature: tree->lz-leaves(tree : Tree)
  101. ; Type: [Tree -> LZL]
  102. ; Purpose: Returns an ordered lazy list of the labels which appear in the leaves of the tree.
  103. ; Precondition: tree is of type Tree
  104. ; Test: (take (tree->lz-leaves '(a (b c)(((x y))))) 5) => '(a b c x y)
  105. (define tree->lz-leaves
  106.   (lambda (tree)
  107.     (list-to-lzlist (tree->leaves tree))
  108. ))
  109.  
  110. ; Signature: lzlists-equal?(lzl1 : LZL, lzl2 : LZL)
  111. ; Type: [LZL * LZL -> Boolean | Pair]
  112. ; Purpose: Returns #t if the lazy lists are equal, otherwise returns a pair of the first unequal elements.
  113. ; Precondition: lzl1, lzl2 are lazy lists.
  114. ; Tests: (lzlists-equal? (list-to-lzlist '(1 2 3)) (list-to-lzlist '(1 2 3))) => #t
  115. ;        (lzlists-equal? (list-to-lzlist '(1 5 3)) (list-to-lzlist '(1 2 3))) => '(5 . 2)
  116. (define lzlists-equal?
  117.   (lambda (lzl1 lzl2)
  118.     (if (and (empty-lzl? lzl1) (empty-lzl? lzl2))
  119.         #t
  120.         (if (or (empty-lzl? lzl1) (empty-lzl? lzl2))
  121.             (if (empty-lzl? lzl1)
  122.                 (cons empty (head lzl2))
  123.                 (cons (head lzl1) empty)
  124.             )
  125.         (if (not (equal? (head lzl1) (head lzl2)))
  126.             (cons (head lzl1) (head lzl2))
  127.             (lzlists-equal? (tail lzl1) (tail lzl2))
  128.         )
  129.         )
  130. )))
  131.  
  132. ; Signature: same-leaves?(t1 : Tree, t1 : Tree)
  133. ; Type: [Tree * Tree -> Boolean | Pair]
  134. ; Purpose: Returns #t if the trees have the same leaves (ordered),
  135. ;          otherwise returns a pair of the first unequal leaves.
  136. ; Precondition: t1, t2 are of type Tree.
  137. ; Tests: (same-leaves? '((a b) c) '(a (b c))); => #t
  138. ;        (same-leaves? '(a (b c)) '(a d c)); => '(b . d)
  139. (define same-leaves?
  140.   (lambda (t1 t2)
  141.     (lzlists-equal? (tree->lz-leaves t1) (tree->lz-leaves t2))
  142. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement