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)
- #| ;;auxiliar functions for testing
- (define (list->p:list l) (foldr (lambda (elem new-l) (delay (cons elem new-l))) (delay empty) l))
- (define (p:list . l) (list->p:list l))
- (define (p:list->list l)
- (define elem (force l))
- (cond [(empty? elem) elem]
- [else (cons (car elem) (p:list->list (cdr elem)))]))
- (define (check-p:list-equal? given expected) (check-equal? (p:list->list given) expected))
- (define (p:list? l)
- (and
- (promise? l)
- (or (empty? (force l)) (p:list? (cdr (force l))))))
- |#
- ;; 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)
- (delay (cons first rest)))
- ;;Following the structure (delay (cons val (delay ( cons val p:empty)))
- ;;We create a p:cons that use the same structure, so when we call
- ;;(define l1 (p:cons 1 (p:cons 2 p:empty)))
- ;;we got #<promise:p:cons> in return, not evaluating 1
- ;; (p:first l1)
- ;; 1
- ;; (p:first (p:rest l1))
- ;; 2
- ;; (p:rest (p:rest l1 ))
- ;;#<promise!()>
- ;; 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)) (delay p:empty)]
- [(p:empty? l1) (delay (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
- #|
- (define (tree left value right) (list left value right))
- (define (tree-leaf value) (tree null value null))
- (define (tree-left self) (first self))
- (define (tree-value self) (second self))
- (define (tree-right self) (third self))
- (define (tree-set-value self value) (tree (tree-left self) value (tree-right self)))
- (define (tree-set-left self left) (tree left (tree-value self) (tree-right self)))
- (define (tree-set-right self right) (tree (tree-left self) (tree-value self) right))
- (define (bst-insert self value)
- (cond
- [(null? self) (tree-leaf value)]
- [(= value (tree-value self)) (tree-set-value self value)]
- [(< value (tree-value self)) (tree-set-left self (bst-insert (tree-left self) value))]
- [else (tree-set-right self (bst-insert (tree-right self) value))]))
- (define (list->bst elems) (foldr (lambda (v t) (bst-insert t v)) null elems))
- (define (bst->list self)
- (cond [(empty? self) self]
- [else
- (append
- (bst->list (tree-left self))
- (cons (tree-value self)
- (bst->list (tree-right self))))]))
- ;; 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
- (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 (value) #:transparent)
- (define (r:eval-builtin sym)
- (cond [(equal? sym '+) +]
- [(equal? sym '*) *]
- [(equal? sym '-) -]
- [(equal? sym '/) /]
- [(equal? sym 'and) r:and]
- [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:bool? exp) (r:bool-value exp)]
- [(r:apply? exp)
- (cond [ (= 1 (length (r:apply-args exp)))
- (and (r:eval-exp (first (r:apply-args exp))))]
- [ (= 0 (length (r:apply-args exp)))
- (if (equal? (r:variable-name (r:apply-func exp)) 'and)
- (and)
- ( (r:eval-builtin (r:variable-name (r:apply-func exp)))))]
- ;; (apply (r:eval-builtin (r:variable-name (r:apply-func exp))) empty))]
- ;;(and)]
- [else
- (foldl (r:eval-exp (r:apply-func exp))
- (r:eval-exp (first (r:apply-args exp)))
- (map r:eval-exp (rest (r:apply-args exp)))) ])]
- ;; (r:eval-exp (rest (r:apply-args exp))) )]
- [else (error "Unknown expression:" exp)]))
- (define (r:and acc arg)
- (and arg acc))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement