Advertisement
Guest User

Untitled

a guest
Mar 9th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 5.39 KB | None | 0 0
  1. #lang racket
  2. (require "ast.rkt")
  3. (provide (all-defined-out))
  4. (require rackunit)
  5.  
  6. #| ;;auxiliar functions for testing
  7. (define (list->p:list l) (foldr (lambda (elem new-l) (delay (cons elem new-l))) (delay empty) l))
  8. (define (p:list . l) (list->p:list l))
  9. (define (p:list->list l)
  10.   (define elem (force l))
  11.   (cond [(empty? elem) elem]
  12.         [else (cons (car elem) (p:list->list (cdr elem)))]))
  13. (define (check-p:list-equal? given expected) (check-equal? (p:list->list given) expected))
  14. (define (p:list? l)
  15.   (and
  16.     (promise? l)
  17.     (or (empty? (force l)) (p:list? (cdr (force l))))))
  18. |#
  19.  
  20. ;; Exercise 1.a
  21. (define p:empty  (delay null))
  22. ;;Create a empty list with delay, creating a promise
  23.  
  24. ;; Exercise 1.b
  25. (define (p:empty? l)
  26.   (empty? (force l )))
  27. ;;Since l is a promise, we need to get the value using force, and check if its null
  28.  
  29. ;;Exercise 1.c
  30.  
  31. (define (p:cons first rest)
  32.   (delay (cons first rest)))
  33.  
  34. ;;Following the structure (delay (cons val (delay ( cons val p:empty)))
  35. ;;We create a p:cons that use the same structure, so when we call
  36. ;;(define l1 (p:cons 1 (p:cons 2 p:empty)))
  37. ;;we got #<promise:p:cons> in return, not evaluating 1
  38. ;; (p:first l1)
  39. ;; 1
  40. ;; (p:first (p:rest l1))
  41. ;; 2
  42. ;; (p:rest (p:rest l1 ))
  43. ;;#<promise!()>
  44.  
  45. ;; Exercise 1.d
  46. (define (p:first l)
  47.   (car (force l)))
  48.  
  49. ;; Exercise 1.e
  50. (define (p:rest l)
  51.   (cdr (force l)))
  52. ;; Exercise 1.f
  53. (define (p:append l1 l2)
  54.   (cond
  55.     [(and (p:empty? l1) (p:empty? l2)) p:empty]
  56.     [(p:empty? l1) (delay (cons (p:first l2) (p:append l1 (p:rest l2))))]
  57.     [else (delay (cons (p:first l1) (p:append (p:rest l1) l2)))]))
  58. ;;If both list are empty, return '(), the final value for cons
  59. ;;If l1 is empty, recursive cons the values from l2
  60. ;;if l1 is not empty, recursive cons the values from l1
  61. #|  
  62. (define (tree left value right) (list left value right))
  63. (define (tree-leaf value) (tree null value null))
  64. (define (tree-left self) (first self))
  65. (define (tree-value self) (second self))
  66. (define (tree-right self) (third self))
  67. (define (tree-set-value self value) (tree (tree-left self) value (tree-right self)))
  68. (define (tree-set-left self left) (tree left (tree-value self) (tree-right self)))
  69. (define (tree-set-right self right) (tree (tree-left self) (tree-value self) right))
  70. (define (bst-insert self value)
  71.   (cond
  72.     [(null? self) (tree-leaf value)]
  73.     [(= value (tree-value self)) (tree-set-value self value)]
  74.     [(< value (tree-value self)) (tree-set-left self (bst-insert (tree-left self) value))]
  75.     [else (tree-set-right self (bst-insert (tree-right self) value))]))
  76. (define (list->bst elems) (foldr (lambda (v t) (bst-insert t v)) null elems))
  77. (define (bst->list self)
  78.   (cond [(empty? self) self]
  79.         [else
  80.          (append
  81.            (bst->list (tree-left self))
  82.            (cons (tree-value self)
  83.                  (bst->list (tree-right self))))]))
  84. ;; Exercise 2.a
  85. ;; Auxiliary functions;;
  86. |#
  87. (define (tree-left self)(p:first self))
  88. (define (tree-value self) (p:first (p:rest self)))
  89. (define (tree-right self) (p:first (p:rest (p:rest self)) ))
  90.  
  91. (define (bst->p:list self)
  92.     (cond [(p:empty? self) self]
  93.           [else(p:append
  94.                 (bst->p:list (tree-left self))
  95.                 (delay (cons (tree-value self)
  96.                              (bst->p:list (tree-right self)))))] ))
  97.  
  98. ;;Just change the previous algorithm with the newly created functions from exercise 1
  99.  
  100. ;;Exercise 3
  101. (define (stream-get stream)  (car stream))
  102. (define (stream-next stream) ((cdr stream)))
  103.  
  104. (define (stream-foldl f a s)
  105.   (define (foldl-aux aux stream)
  106.     (thunk
  107.      (cons
  108.       aux
  109.       (foldl-aux (f (stream-get stream) aux) (stream-next stream))) ))
  110.   ( (foldl-aux a s)))
  111. ;;All stream values must have the format (thunk (cons val rest ))
  112. ;;So we call f with accumulated value and currenctly stream value
  113. ;;Pass this value to acc and make a recursive call with the next value
  114.  
  115.  
  116. ;; Exercise 4
  117. ;; There is one solution with 3 lines of code.
  118. (define (stream-skip n s)
  119.     (if (equal? n 1)
  120.         (stream-next s )
  121.         (stream-skip (- n 1) (stream-next s))))
  122. ;;Iterate n times
  123. ;;When we got n = 1 we return the rest of the strea
  124.  
  125.  
  126. ;; Exercise 5
  127. (struct r:bool (value) #:transparent)
  128. (define (r:eval-builtin sym)
  129.   (cond [(equal? sym '+) +]
  130.         [(equal? sym '*) *]
  131.         [(equal? sym '-) -]
  132.         [(equal? sym '/) /]
  133.         [(equal? sym 'and) r:and]
  134.         [else #f]))
  135.  
  136. (define (r:eval-exp exp)
  137.   (cond
  138.     ; 1. When evaluating a number, just return that number
  139.     [(r:number? exp) (r:number-value exp)]
  140.     ; 2. When evaluating an arithmetic symbol,
  141.     ;    return the respective arithmetic function
  142.     [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
  143.     ; 3. When evaluating a function call evaluate each expression and apply
  144.     ;    the first expression to remaining ones
  145.     [(r:bool? exp) (r:bool-value exp)]
  146.     [(r:apply? exp)
  147.      (cond [ (= 1 (length (r:apply-args exp)))
  148.              (and (r:eval-exp (first (r:apply-args exp))))]
  149.            [ (= 0 (length (r:apply-args exp)))
  150.              (and)]
  151.            [else
  152.             (foldl (r:eval-exp (r:apply-func exp))
  153.                    (r:eval-exp (first (r:apply-args exp)))
  154.                    (map r:eval-exp (rest (r:apply-args exp)))) ])]
  155.            ;;  (r:eval-exp (rest (r:apply-args exp))) )]
  156.     [else (error "Unknown expression:" exp)]))
  157.  
  158. (define (r:and acc arg)
  159.   (and arg acc))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement