Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.83 KB | None | 0 0
  1. #lang racket
  2.  
  3. (require "ast.rkt")
  4. (provide (all-defined-out))
  5. (require rackunit)
  6. ;; Exercise 1.a
  7. ;; There is one solution with 1 line of code.
  8. (define p:empty  (delay null))
  9. ;; Exercise 1.b
  10. ;; There is one solution with 1 line of code
  11.  
  12. (define (p:empty? l)
  13.   (empty? (force l )))
  14. ;; Exercise 1.d
  15. ;; There is one solution with 1 line of code.
  16. (define (p:first l)
  17.   (car (force l)))
  18. ;; Exercise 1.e
  19. ;; There is one solution with 1 line of code
  20. (define (p:rest l)
  21.   (cdr (force l)))
  22. ;; Exercise 1.f
  23. ;; There is one solution with 4 lines of code.
  24. (define (p:append l1 l2)
  25.   (cond
  26.     [(and (p:empty? l1) (p:empty? l2)) p:empty]
  27.     [(p:empty? l1) (cons (p:first l2) (p:append l1 (p:rest l2)))]
  28.     [else (delay (cons (p:first l1) (p:append (p:rest l1) l2)))]))
  29.        
  30.  
  31. ;; Exercise 2.a
  32. ;; Auxiliary functions;;
  33. (define (tree-left self)(p:first self))
  34. (define (tree-value self) (p:first (p:rest self)))
  35. (define (tree-right self) (p:first (p:rest (p:rest self)) ))
  36. ;; There is one solution with 10 lines of code.
  37. ;;(define (bst->p:list self) 'todo)
  38. (define (bst->p:list self)
  39.     (cond [(p:empty? self) self]
  40.           [else
  41.            (p:append
  42.              (bst->p:list (tree-left self))
  43.              (delay (cons (tree-value self)
  44.                    (bst->p:list (tree-right self)))))]))
  45.  
  46. ;; Exercise 3
  47. ;; Auxiliary functions
  48.  
  49. (define (naturals)
  50.   (define (naturals-iter n)
  51.     (thunk
  52.      (cons n (naturals-iter (+ n 1)))))
  53.   ( (naturals-iter 0)))
  54. (define (stream-get stream)  (car stream))
  55. (define (stream-next stream) ((cdr stream)))
  56.  
  57. (define (stream-foldl f a s)
  58.   (define (foldl-aux aux stream)
  59.     (thunk
  60.      (cons
  61.       aux
  62.       (foldl-aux (f (stream-get stream) aux) (stream-next stream))) ))
  63.   ( (foldl-aux a s)))
  64.                  
  65.  
  66. ;; Exercise 4
  67. ;; There is one solution with 3 lines of code.
  68. (define (stream-skip n s)
  69.     (if (equal? n 1)
  70.         (stream-next s )
  71.         (stream-skip (- n 1) (stream-next s))))
  72.  
  73.  
  74.  
  75. ;; Exercise 5
  76. (define (r:eval-builtin sym)
  77.   (cond [(equal? sym '+) +]
  78.         [(equal? sym '*) *]
  79.         [(equal? sym '-) -]
  80.         [(equal? sym '/) /]
  81.         [else #f]))
  82. (define r:bool 'todo)
  83. (define r:bool? 'todo)
  84. (define r:bool-value 'todo)
  85. (define (r:eval-exp exp)
  86.   (cond
  87.     ; 1. When evaluating a number, just return that number
  88.     [(r:number? exp) (r:number-value exp)]
  89.     ; 2. When evaluating an arithmetic symbol,
  90.     ;    return the respective arithmetic function
  91.     [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
  92.     ; 3. When evaluating a function call evaluate each expression and apply
  93.     ;    the first expression to remaining ones
  94.     [(r:apply? exp)
  95.      ((r:eval-exp (r:apply-func exp))
  96.       (r:eval-exp (first (r:apply-args exp)))
  97.       (r:eval-exp (second (r:apply-args exp))))]
  98.     [else (error "Unknown expression:" exp)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement