Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.28 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.   (cons (delay first rest)))
  20. |#;; Exercise 1.d
  21. (define (p:first l)
  22.   (car (force l)))
  23.  
  24. ;; Exercise 1.e
  25. (define (p:rest l)
  26.   (cdr (force l)))
  27. ;; Exercise 1.f
  28. (define (p:append l1 l2)
  29.   (cond
  30.     [(and (p:empty? l1) (p:empty? l2)) p:empty]
  31.     [(p:empty? l1) (cons (p:first l2) (p:append l1 (p:rest l2)))]
  32.     [else (delay (cons (p:first l1) (p:append (p:rest l1) l2)))]))
  33. ;;If both list are empty, return '(), the final value for cons
  34. ;;If l1 is empty, recursive cons the values from l2
  35. ;;if l1 is not empty, recursive cons the values from l1
  36.  
  37.        
  38.  
  39. ;; Exercise 2.a
  40. ;; Auxiliary functions;;
  41. (define (tree-left self)(p:first self))
  42. (define (tree-value self) (p:first (p:rest self)))
  43. (define (tree-right self) (p:first (p:rest (p:rest self)) ))
  44.  
  45. (define (bst->p:list self)
  46.     (cond [(p:empty? self) self]
  47.           [else
  48.            (p:append
  49.              (bst->p:list (tree-left self))
  50.              (delay (cons (tree-value self)
  51.                    (bst->p:list (tree-right self)))))]))
  52. ;;Just change the previous algorithm with the newly created functions from exercise 1
  53.  
  54. ;; Exercise 3
  55. ;; Auxiliary functions
  56. #|
  57. (define (naturals)
  58.   (define (naturals-iter n)
  59.     (thunk
  60.      (cons n (naturals-iter (+ n 1)))))
  61.   ( (naturals-iter 0)))
  62. |#
  63. (define (stream-get stream)  (car stream))
  64. (define (stream-next stream) ((cdr stream)))
  65.  
  66. (define (stream-foldl f a s)
  67.   (define (foldl-aux aux stream)
  68.     (thunk
  69.      (cons
  70.       aux
  71.       (foldl-aux (f (stream-get stream) aux) (stream-next stream))) ))
  72.   ( (foldl-aux a s)))
  73. ;;All stream values must have the format (thunk (cons val rest ))
  74. ;;So we call f with accumulated value and currenctly stream value
  75. ;;Pass this value to acc and make a recursive call with the next value
  76.  
  77.  
  78. ;; Exercise 4
  79. ;; There is one solution with 3 lines of code.
  80. (define (stream-skip n s)
  81.     (if (equal? n 1)
  82.         (stream-next s )
  83.         (stream-skip (- n 1) (stream-next s))))
  84. ;;Iterate n times
  85. ;;When we got n = 1 we return the rest of the strea
  86.  
  87.  
  88. ;; Exercise 5
  89. (struct r:bool (val) #:transparent)
  90. ;;(define r:bool? e
  91.  ;;(or #t #f))
  92. (define r:bool-value 'todo)
  93.  
  94. (define (r:eval-builtin sym)
  95.   (cond [(equal? sym '+) +]
  96.         [(equal? sym '*) *]
  97.         [(equal? sym '-) -]
  98.         [(equal? sym '/) /]
  99.         [else #f]))
  100.  
  101. (define (r:eval-exp exp)
  102.   (cond
  103.     ; 1. When evaluating a number, just return that number
  104.     [(r:number? exp) (r:number-value exp)]
  105.     ; 2. When evaluating an arithmetic symbol,
  106.     ;    return the respective arithmetic function
  107.     [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
  108.     ; 3. When evaluating a function call evaluate each expression and apply
  109.     ;    the first expression to remaining ones
  110.     [(r:apply? exp)
  111.      ((r:eval-exp (r:apply-func exp))
  112.       (r:eval-exp (first (r:apply-args exp)))
  113.       (r:eval-exp (second (r:apply-args exp))))]
  114.     [else (error "Unknown expression:" exp)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement