Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 4.09 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.  
  63. ;; Exercise 2.a
  64. ;; Auxiliary functions;;
  65. (define (tree-left self)(p:first self))
  66. (define (tree-value self) (p:first (p:rest self)))
  67. (define (tree-right self) (p:first (p:rest (p:rest self)) ))
  68.  
  69. (define (bst->p:list self)
  70.     (cond [(p:empty? self) self]
  71.           [else
  72.            (p:append
  73.              (bst->p:list (tree-left self))
  74.              (delay (cons (tree-value self)
  75.                    (bst->p:list (tree-right self)))))]))
  76. ;;Just change the previous algorithm with the newly created functions from exercise 1
  77.  
  78. ;;Exercise 3
  79. (define (stream-get stream)  (car stream))
  80. (define (stream-next stream) ((cdr stream)))
  81.  
  82. (define (stream-foldl f a s)
  83.   (define (foldl-aux aux stream)
  84.     (thunk
  85.      (cons
  86.       aux
  87.       (foldl-aux (f (stream-get stream) aux) (stream-next stream))) ))
  88.   ( (foldl-aux a s)))
  89. ;;All stream values must have the format (thunk (cons val rest ))
  90. ;;So we call f with accumulated value and currenctly stream value
  91. ;;Pass this value to acc and make a recursive call with the next value
  92.  
  93.  
  94. ;; Exercise 4
  95. ;; There is one solution with 3 lines of code.
  96. (define (stream-skip n s)
  97.     (if (equal? n 1)
  98.         (stream-next s )
  99.         (stream-skip (- n 1) (stream-next s))))
  100. ;;Iterate n times
  101. ;;When we got n = 1 we return the rest of the strea
  102.  
  103.  
  104. ;; Exercise 5
  105. (struct r:bool (value) #:transparent)
  106. (define (r:eval-builtin sym)
  107.   (cond [(equal? sym '+) +]
  108.         [(equal? sym '*) *]
  109.         [(equal? sym '-) -]
  110.         [(equal? sym '/) /]
  111.         [(equal? sym 'and) r:and]
  112.         [else #f]))
  113.  
  114. (define (r:eval-exp exp)
  115.   (cond
  116.     ; 1. When evaluating a number, just return that number
  117.     [(r:number? exp) (r:number-value exp)]
  118.     ; 2. When evaluating an arithmetic symbol,
  119.     ;    return the respective arithmetic function
  120.     [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
  121.     ; 3. When evaluating a function call evaluate each expression and apply
  122.     ;    the first expression to remaining ones
  123.     [(r:bool? exp) (r:bool-value exp)]
  124.     [(r:apply? exp)
  125.      (foldl (r:eval-exp (r:apply-func exp))
  126.             (r:eval-exp (first (r:apply-args exp)))
  127.             (map r:eval-exp (rest (r:apply-args exp))))]
  128.            ;;  (r:eval-exp (rest (r:apply-args exp))) )]
  129.     [else (error "Unknown expression:" exp)]))
  130.  
  131. (define (r:and acc arg)
  132.   (and arg acc))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement