Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (interleave list1 list2)
  4. (if (null? list1) ; If list1 is empty, just return list2
  5. list2
  6. (cons (car list1) ; Otherwise, add the first element of list1 to a recursive call:
  7. (interleave list2 (cdr list1)); pass in list2 as the first list, this way we alternate between the lists. Then pass in the rest of list1.
  8. )
  9. )
  10. )
  11.  
  12.  
  13.  
  14. (define (key-store list1 key)
  15. (if (null? list1) ; If we never found the key, return an empty list
  16. '()
  17. (cond
  18. [(equal? (car (car list1)) key) ; If the key of the first element is equal to the serach key
  19. (car (cdr (car list1))) ; Return the second element of the first list in list1
  20. ]
  21. [else
  22. (key-store (cdr list1) key) ; Otherwise, recursively call the function, passing in the next elements of the list
  23. ]
  24. )
  25. )
  26. )
  27.  
  28.  
  29. ;TODO
  30. (define (list-replace list1 key value)
  31. (if (null? list1)
  32. '()
  33. (cond
  34. [(equal? (car list1) key)
  35. (cons value (cdr list1)) ; possibly change to (member ...)
  36. ]
  37. [(list? (car list1))
  38. (list-replace (car list1) key value) ; Issue is that this is an if/else, not an if.
  39. ]
  40. [else
  41. (list-replace (cdr list1) key value)
  42. ]
  43. )
  44. )
  45. )
  46.  
  47.  
  48.  
  49. (define (first-n list1 num)
  50. (if (<= num 0) ; base case
  51. '()
  52. (if (>= num (length list1))
  53. list1
  54. (cons (car list1) (first-n (cdr list1) (- num 1)))
  55. )
  56. )
  57. )
  58.  
  59.  
  60. ;TODO
  61. (define (key-store-increment list1 key)
  62. (if (null? list1)
  63. (append (list key) (list 1))
  64. (cond
  65. [(equal? (car (car list1)) key)
  66. (append (list (car (car list1))) (list (+ (car (cdr (car list1))) 1)))
  67. ][else
  68. (key-store-increment (cdr list1) key) ])))
  69.  
  70.  
  71.  
  72. (define (indices list1 x)
  73. (indices-helper list1 x 0))
  74.  
  75. (define (indices-helper list1 x counter)
  76. (if (null? list1) ;base case
  77. '()
  78. (cond
  79. [(equal? (car list1) x) ; If we found a value equal to x
  80. (cons counter (indices-helper (cdr list1) x (+ counter 1)))] ;add the current index to a recursive-call(pass in the next part of the list, and an incremented counter)
  81. [else
  82. (indices-helper (cdr list1) x (+ counter 1))] ; recursively call, passing in the next part of the list and incrementing counter
  83. )
  84. )
  85. )
  86.  
  87.  
  88.  
  89. (define (join-together list1 list2)
  90. (if (null? list1)
  91. list2
  92. (if (null? list2)
  93. list1
  94. (if (< (car list1) (car list2))
  95. (cons (car list1) (join-together (cdr list1) list2))
  96. (cons (car list2) (join-together list1 (cdr list2)))
  97. )
  98. )
  99. )
  100. )
  101.  
  102. (define (merge-sorter list1)
  103. (if (null? list1)
  104. '() ; If empty
  105. (if (null? (cdr list1))
  106. list1 ; If one element
  107. (merge (merge-sorter(car (split list1)))
  108. (merge-sorter(car (cdr list1))))
  109. )
  110. )
  111. )
  112.  
  113. (define (split-list list1)
  114. (cons (even-indices list1) (list (odd-indices list1))))
  115.  
  116.  
  117. ; Return each even-index as a list
  118. (define (even-indices list1)
  119. (if (null? list1)
  120. '()
  121. (if (null? (cdr list1))
  122. '()
  123. (odd-indices (cdr list1)))))
  124.  
  125.  
  126. ; Return each odd-index as a list
  127. (define (odd-indices list1)
  128. (if (null? list1)
  129. '()
  130. (if (null? (cdr list1))
  131. (list (car list1))
  132. (cons (car list1) (odd-indices (cdr (cdr list1)))))))
  133.  
  134. ;; Exp. (merge '(1 3 5 7 8 9 10) '(2 4 6)) ==> (1 2 3 4 5 6 7 8 9 10)
  135. (define (merge L M)
  136. (if (null? L) M
  137. (if (null? M) L
  138. (if (< (car L) (car M))
  139. (cons (car L) (merge (cdr L) M))
  140. (cons (car M) (merge (cdr M) L))))))
  141.  
  142. ;; split helper functions
  143. (define (odd L)
  144. (if (null? L) '()
  145. (if (null? (cdr L)) (list (car L))
  146. (cons (car L) (odd (cddr L))))))
  147. (define (even L)
  148. (if (null? L) '()
  149. (if (null? (cdr L)) '()
  150. (cons (cadr L) (even (cddr L))))))
  151.  
  152. ;; Exp. (split '(a b c d e f g h i)) ==> ((a c e g i)(b d f h))
  153. (define (split L)
  154. (cons (odd L) (cons (even L) `())))
  155.  
  156. ;; Exp. (mergesort '(8 1 3 9 6 5 7 2 4 10)) ==> (1 2 3 4 5 6 7 8 9 10)
  157. (define (mergesort L)
  158. (if (null? L) L
  159. (if (null? (cdr L)) L
  160. (merge
  161. (mergesort (car (split L)))
  162. (mergesort (cadr (split L)))))))
  163.  
  164. For
  165. For each emotion in my emotions:
  166. If emotion is frustration:
  167. Print "I'm frustrated because…"
  168. Printing…
  169. What are you talking about?
  170. If emotion is confusion:
  171. Print "I'm confused because you said…"
  172. Printing…
  173. What the hell do you mean?
  174. If emotion is anger:
  175. Print "I'm angry because you said I didn't listen…"
  176. Printing…
  177. What the fuck? I never did that?
  178. If emotion is sadness:
  179. Print "I'm sad because you said I didn't listen when you were telling me about…"
  180. Printing…
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement