Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.96 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. ;; There is one solution with 6 lines of code.
  57. #|(define (stream-foldl f a s)
  58.   (define (foldl-aux aux stream)
  59.     (thunk
  60.      (cons (f (stream-get s) a)
  61.      (foldl-aux (stream-next stream)))))
  62.   ((foldl-aux s)))
  63.        |#
  64.  
  65. (define (stream-foldl f a s)
  66.   (define (foldl-aux aux stream)
  67.     (thunk
  68.      (cons
  69.       aux
  70.       (foldl-aux (f (stream-get stream) aux) (stream-next stream))) ))
  71.   ( (foldl-aux a s)))
  72.            
  73.      
  74.  
  75. ;; Exercise 4
  76. ;; There is one solution with 3 lines of code.
  77. (define (stream-skip n s) 'todo)
  78.  
  79. ;; Exercise 5
  80. (define (r:eval-builtin sym)
  81.   (cond [(equal? sym '+) +]
  82.         [(equal? sym '*) *]
  83.         [(equal? sym '-) -]
  84.         [(equal? sym '/) /]
  85.         [else #f]))
  86. (define r:bool 'todo)
  87. (define r:bool? 'todo)
  88. (define r:bool-value 'todo)
  89. (define (r:eval-exp exp)
  90.   (cond
  91.     ; 1. When evaluating a number, just return that number
  92.     [(r:number? exp) (r:number-value exp)]
  93.     ; 2. When evaluating an arithmetic symbol,
  94.     ;    return the respective arithmetic function
  95.     [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
  96.     ; 3. When evaluating a function call evaluate each expression and apply
  97.     ;    the first expression to remaining ones
  98.     [(r:apply? exp)
  99.      ((r:eval-exp (r:apply-func exp))
  100.       (r:eval-exp (first (r:apply-args exp)))
  101.       (r:eval-exp (second (r:apply-args exp))))]
  102.     [else (error "Unknown expression:" exp)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement