Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.61 KB | None | 0 0
  1. #lang racket
  2. (require rackunit)
  3. (require "ast.rkt")
  4. (provide (all-defined-out))
  5.  
  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. (define (stream-get stream) (car stream))
  49. (define (stream-next stream) (cdr stream))
  50. ;; There is one solution with 6 lines of code.
  51. (define (stream-foldl f a s)
  52.   (define (foldl-iter acc s)
  53.     (if (empty? s) acc
  54.         (foldl-iter (f (stream-get s) acc) (stream-next s))))
  55.   (foldl-iter a s ))
  56.  
  57. ;; Exercise 4
  58. ;; There is one solution with 3 lines of code.
  59. (define (stream-skip n s) 'todo)
  60. (define r:bool 'todo)
  61. (define r:bool? 'todo)
  62. (define r:bool-value 'todo)
  63. ;; Exercise 5
  64. (define (r:eval-builtin sym)
  65.   (cond [(equal? sym '+) +]
  66.         [(equal? sym '*) *]
  67.         [(equal? sym '-) -]
  68.         [(equal? sym '/) /]
  69.         [else #f]))
  70.  
  71. (define (r:eval-exp exp)
  72.   (cond
  73.     ; 1. When evaluating a number, just return that number
  74.     [(r:number? exp) (r:number-value exp)]
  75.     ; 2. When evaluating an arithmetic symbol,
  76.     ;    return the respective arithmetic function
  77.     [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
  78.     ; 3. When evaluating a function call evaluate each expression and apply
  79.     ;    the first expression to remaining ones
  80.     [(r:apply? exp)
  81.      ((r:eval-exp (r:apply-func exp))
  82.       (r:eval-exp (first (r:apply-args exp)))
  83.       (r:eval-exp (second (r:apply-args exp))))]
  84.     [else (error "Unknown expression:" exp)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement