Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. ; Let's imagine we have a list of relative distances
  2. ; between cities along a road,
  3. ; and we want to convert them to a list of
  4. ; absolute distances. For example, we might have
  5.  
  6. ;(list 10 50 20)
  7.  
  8. ; and we would want to convert this to
  9.  
  10. ;(list 10 60 80)
  11.  
  12. ; Design this as the function relative->absolute
  13.  
  14. ; relative->absolute : [List-of Number] -> [List-of Number]
  15. ; See above
  16.  
  17. (check-expect (relative->absolute empty) empty)
  18. (check-expect (relative->absolute (list 10 50 20)) (list 10 60 80))
  19.  
  20. (define (relative->absolute l)
  21. (local [; relative->absolute/acc : [List-of Number] Number -> [List-of Number]
  22. ; converts relative to absolute given distance traveled so far
  23. ; Accumulator: represents the distance traveled so far
  24. (define (relative->absolute/acc l distance-so-far)
  25. (cond
  26. [(empty? l) empty]
  27. [(cons? l)
  28. (cons
  29. (+ (first l) distance-so-far)
  30. (relative->absolute/acc (rest l) (+ (first l) distance-so-far)))]))]
  31. (relative->absolute/acc l 0)))
  32.  
  33. #|
  34. (define (foo a b c)
  35. (local [; SIG
  36. ; PURPOSE
  37. ; Acuumulator: what the accumulator represents
  38. (define (foo/acc x y z acc)
  39. ...)]
  40. (foo/acc a b c acc0)))
  41. |#
  42.  
  43. ; Design to10 which consumes a list of digits
  44. ; and produces the corresponding number.
  45. ; For example, when applied to (list 1 0 2),
  46. ; it produces 102. First design it without an accumulator
  47.  
  48. ; to10 : [List-of Nat[0, 9]] -> Nat
  49. ; takes a list of numbers and transforms it into one number
  50.  
  51. (check-expect (to10 empty) 0)
  52. (check-expect (to10 (list 1 0 2)) 102)
  53. (check-expect (to10 (list 5 0 2 0 9)) 50209)
  54.  
  55. (check-expect (to10-2 empty) 0)
  56. (check-expect (to10-2 (list 1 0 2)) 102)
  57. (check-expect (to10-2 (list 5 0 2 0 9)) 50209)
  58.  
  59. (check-expect (to10-3 empty) 0)
  60. (check-expect (to10-3 (list 1 0 2)) 102)
  61. (check-expect (to10-3 (list 5 0 2 0 9)) 50209)
  62.  
  63. (define (to10 l)
  64. (cond
  65. [(empty? l) 0]
  66. [(cons? l)
  67. (+
  68. (* (first l) (expt 10 (length (rest l))))
  69. (to10 (rest l)))]))
  70.  
  71. (define (to10-2 l)
  72. (cond
  73. [(empty? l) 0]
  74. [(cons? l)
  75. (string->number (implode (map number->string l)))]))
  76.  
  77. (define (to10-3 l)
  78. (local [; to10-3/acc : [List-of Nat[0, 9] Nat -> Nat
  79. ; Converts the list given the current exponential multiplier
  80. ; Acuumulator: The Amount of digits within the
  81. (define (to10-3/acc l acc)
  82. (cond
  83. [(empty? l) 0]
  84. [(cons? l)
  85. (+
  86. (* (first l) acc)
  87. (to10-3/acc (rest l) (/ acc 10)))]))]
  88. (to10-3/acc l (expt 10 (sub1 (length l))))))
  89.  
  90. #|
  91. As a review design the function f0ldr...
  92. (foldr f base (cons A (cons B (cons C empty))))
  93. =
  94. (f A (f B (f C base)))
  95. |#
  96.  
  97. ; f0ldr : (X Y) [X Y -> Y] Y [List-of X] -> Y
  98. ; Iterative applies the funftion starting from the right
  99.  
  100. (check-expect (f0ldr string-append "XXX" empty) "XXX")
  101. (check-expect (f0ldr string-append "XXX"( list "a" "b" "c")) "abcXXX")
  102.  
  103. (define (f0ldr f base lox)
  104. (cond
  105. [(empty? lox) base]
  106. [(cons? lox)
  107. (f
  108. (first lox)
  109. (f0ldr f base (rest lox)))]))
  110.  
  111. ; f0ldl : (X Y) [X Y -> Y] Y [List-of X] -> Y
  112. ; Iterative applies the funftion starting from the left
  113.  
  114. (check-expect (f0ldl string-append "XXX" empty) "XXX")
  115. (check-expect (f0ldl string-append "XXX" (list "a" "b" "c")) "cbaXXX")
  116.  
  117. (define (f0ldl f base lox)
  118. (local [; f0ldl/acc : (X Y) [X Y -> Y] Y [List-of X] Y -> Y
  119. ; Implements foldr, given the accumulator
  120. ; Accumulator: Folded result so far
  121. (define (f0ldl/acc f base lox result-so-far)
  122. (cond
  123. [(empty? lox) result-so-far]
  124. [(cons? lox)
  125. (f0ldl/acc f base (rest lox) (f (first lox) result-so-far))]))]
  126. (f0ldl/acc f base lox base)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement