Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (require "ast.rkt")
- (provide (all-defined-out))
- (require rackunit)
- ;; Exercise 1.a
- (define p:empty (delay null))
- ;;Create a empty list with delay, creating a promise
- ;; Exercise 1.b
- (define (p:empty? l)
- (empty? (force l )))
- ;;Since l is a promise, we need to get the value using force, and check if its null
- ;;Exercise 1.c
- #|
- (define (p:cons first rest)
- (cons (delay first rest)))
- |#;; Exercise 1.d
- (define (p:first l)
- (car (force l)))
- ;; Exercise 1.e
- (define (p:rest l)
- (cdr (force l)))
- ;; Exercise 1.f
- (define (p:append l1 l2)
- (cond
- [(and (p:empty? l1) (p:empty? l2)) p:empty]
- [(p:empty? l1) (cons (p:first l2) (p:append l1 (p:rest l2)))]
- [else (delay (cons (p:first l1) (p:append (p:rest l1) l2)))]))
- ;;If both list are empty, return '(), the final value for cons
- ;;If l1 is empty, recursive cons the values from l2
- ;;if l1 is not empty, recursive cons the values from l1
- ;; Exercise 2.a
- ;; Auxiliary functions;;
- (define (tree-left self)(p:first self))
- (define (tree-value self) (p:first (p:rest self)))
- (define (tree-right self) (p:first (p:rest (p:rest self)) ))
- (define (bst->p:list self)
- (cond [(p:empty? self) self]
- [else
- (p:append
- (bst->p:list (tree-left self))
- (delay (cons (tree-value self)
- (bst->p:list (tree-right self)))))]))
- ;;Just change the previous algorithm with the newly created functions from exercise 1
- ;; Exercise 3
- ;; Auxiliary functions
- #|
- (define (naturals)
- (define (naturals-iter n)
- (thunk
- (cons n (naturals-iter (+ n 1)))))
- ( (naturals-iter 0)))
- |#
- (define (stream-get stream) (car stream))
- (define (stream-next stream) ((cdr stream)))
- (define (stream-foldl f a s)
- (define (foldl-aux aux stream)
- (thunk
- (cons
- aux
- (foldl-aux (f (stream-get stream) aux) (stream-next stream))) ))
- ( (foldl-aux a s)))
- ;;All stream values must have the format (thunk (cons val rest ))
- ;;So we call f with accumulated value and currenctly stream value
- ;;Pass this value to acc and make a recursive call with the next value
- ;; Exercise 4
- ;; There is one solution with 3 lines of code.
- (define (stream-skip n s)
- (if (equal? n 1)
- (stream-next s )
- (stream-skip (- n 1) (stream-next s))))
- ;;Iterate n times
- ;;When we got n = 1 we return the rest of the strea
- ;; Exercise 5
- (struct r:bool (val) #:transparent)
- ;;(define r:bool? e
- ;;(or #t #f))
- (define r:bool-value 'todo)
- (define (r:eval-builtin sym)
- (cond [(equal? sym '+) +]
- [(equal? sym '*) *]
- [(equal? sym '-) -]
- [(equal? sym '/) /]
- [else #f]))
- (define (r:eval-exp exp)
- (cond
- ; 1. When evaluating a number, just return that number
- [(r:number? exp) (r:number-value exp)]
- ; 2. When evaluating an arithmetic symbol,
- ; return the respective arithmetic function
- [(r:variable? exp) (r:eval-builtin (r:variable-name exp))]
- ; 3. When evaluating a function call evaluate each expression and apply
- ; the first expression to remaining ones
- [(r:apply? exp)
- ((r:eval-exp (r:apply-func exp))
- (r:eval-exp (first (r:apply-args exp)))
- (r:eval-exp (second (r:apply-args exp))))]
- [else (error "Unknown expression:" exp)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement