Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Let's imagine we have a list of relative distances
- ; between cities along a road,
- ; and we want to convert them to a list of
- ; absolute distances. For example, we might have
- ;(list 10 50 20)
- ; and we would want to convert this to
- ;(list 10 60 80)
- ; Design this as the function relative->absolute
- ; relative->absolute : [List-of Number] -> [List-of Number]
- ; See above
- (check-expect (relative->absolute empty) empty)
- (check-expect (relative->absolute (list 10 50 20)) (list 10 60 80))
- (define (relative->absolute l)
- (local [; relative->absolute/acc : [List-of Number] Number -> [List-of Number]
- ; converts relative to absolute given distance traveled so far
- ; Accumulator: represents the distance traveled so far
- (define (relative->absolute/acc l distance-so-far)
- (cond
- [(empty? l) empty]
- [(cons? l)
- (cons
- (+ (first l) distance-so-far)
- (relative->absolute/acc (rest l) (+ (first l) distance-so-far)))]))]
- (relative->absolute/acc l 0)))
- #|
- (define (foo a b c)
- (local [; SIG
- ; PURPOSE
- ; Acuumulator: what the accumulator represents
- (define (foo/acc x y z acc)
- ...)]
- (foo/acc a b c acc0)))
- |#
- ; Design to10 which consumes a list of digits
- ; and produces the corresponding number.
- ; For example, when applied to (list 1 0 2),
- ; it produces 102. First design it without an accumulator
- ; to10 : [List-of Nat[0, 9]] -> Nat
- ; takes a list of numbers and transforms it into one number
- (check-expect (to10 empty) 0)
- (check-expect (to10 (list 1 0 2)) 102)
- (check-expect (to10 (list 5 0 2 0 9)) 50209)
- (check-expect (to10-2 empty) 0)
- (check-expect (to10-2 (list 1 0 2)) 102)
- (check-expect (to10-2 (list 5 0 2 0 9)) 50209)
- (check-expect (to10-3 empty) 0)
- (check-expect (to10-3 (list 1 0 2)) 102)
- (check-expect (to10-3 (list 5 0 2 0 9)) 50209)
- (define (to10 l)
- (cond
- [(empty? l) 0]
- [(cons? l)
- (+
- (* (first l) (expt 10 (length (rest l))))
- (to10 (rest l)))]))
- (define (to10-2 l)
- (cond
- [(empty? l) 0]
- [(cons? l)
- (string->number (implode (map number->string l)))]))
- (define (to10-3 l)
- (local [; to10-3/acc : [List-of Nat[0, 9] Nat -> Nat
- ; Converts the list given the current exponential multiplier
- ; Acuumulator: The Amount of digits within the
- (define (to10-3/acc l acc)
- (cond
- [(empty? l) 0]
- [(cons? l)
- (+
- (* (first l) acc)
- (to10-3/acc (rest l) (/ acc 10)))]))]
- (to10-3/acc l (expt 10 (sub1 (length l))))))
- #|
- As a review design the function f0ldr...
- (foldr f base (cons A (cons B (cons C empty))))
- =
- (f A (f B (f C base)))
- |#
- ; f0ldr : (X Y) [X Y -> Y] Y [List-of X] -> Y
- ; Iterative applies the funftion starting from the right
- (check-expect (f0ldr string-append "XXX" empty) "XXX")
- (check-expect (f0ldr string-append "XXX"( list "a" "b" "c")) "abcXXX")
- (define (f0ldr f base lox)
- (cond
- [(empty? lox) base]
- [(cons? lox)
- (f
- (first lox)
- (f0ldr f base (rest lox)))]))
- ; f0ldl : (X Y) [X Y -> Y] Y [List-of X] -> Y
- ; Iterative applies the funftion starting from the left
- (check-expect (f0ldl string-append "XXX" empty) "XXX")
- (check-expect (f0ldl string-append "XXX" (list "a" "b" "c")) "cbaXXX")
- (define (f0ldl f base lox)
- (local [; f0ldl/acc : (X Y) [X Y -> Y] Y [List-of X] Y -> Y
- ; Implements foldr, given the accumulator
- ; Accumulator: Folded result so far
- (define (f0ldl/acc f base lox result-so-far)
- (cond
- [(empty? lox) result-so-far]
- [(cons? lox)
- (f0ldl/acc f base (rest lox) (f (first lox) result-so-far))]))]
- (f0ldl/acc f base lox base)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement