Advertisement
Guest User

Untitled

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