Advertisement
Guest User

Untitled

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