Advertisement
Guest User

Untitled

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