Advertisement
Guest User

Untitled

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