Advertisement
Guest User

Untitled

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