Advertisement
Guest User

week5.hoon

a guest
Aug 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. PART 1
  2. :: Define a gate that takes an atom with face 'n' as the argument
  3. ::
  4. |= n=@
  5. :: This rune evaluates the second child expression before executing the first child expression with the result as the subject. This allows the first child expression to reference faces in the second child expression. The first child expression is calling the arm 'goldbach' with 'n' as the argument
  6. ::
  7. =< (goldbach n)
  8. :: This rune produces a core
  9. ::
  10. |%
  11. :: This is the first arm definition of the core with face 'prime'
  12. ::
  13. ++ prime
  14. :: It defines a gate that accepts an atom with face 'n'
  15. ::
  16. |= n=@
  17. :: This rune casts the gate to the boolean type
  18. ::
  19. ^- ?
  20. :: This conditional evaluates to false if 'n' is < 2
  21. ::
  22. ?: (lth n 2) |
  23. :: Else if 'n' is less than 4 it evaultes to true.
  24. ::
  25. ?: (lth n 4) &
  26. :: Otherwise, continue to store the value 2 as an atom with face 'i'
  27. ::
  28. =/ i=@ 2
  29. :: Store the value 2 as an atom with face 'j'
  30. ::
  31. =/ j=@ 2
  32. :: Create a trap as the point of recursion which evaluates to a boolean
  33. ::
  34. |- ^- ?
  35. :: If the product of 'i' and 'j' is equal to 'n', evalute to false.
  36. ::
  37. ?: =((mul i j) n) |
  38. :: If 'n' divided by 2 equals 'j', evalute to true
  39. ::
  40. ?: =(j (div n 2)) &
  41. :: Check if the product of 'i' and 'j' is > 'n'
  42. ::
  43. ?: (gth (mul i j) n)
  44. :: If it is, then make a recursive call back to the trap where i is 2 an j is incremented by 1
  45. ::
  46. $(i 2, j +(j))
  47. :: Otherwise, make a recursive call back to the trap where 'i' is incremented by 1
  48. ::
  49. $(i +(i))
  50. :: This defines the second arm in the core with face 'goldbach'
  51. ::
  52. ++ goldbach
  53. :: Define a gate that takes an atom with face 'n' as the argument
  54. ::
  55. |= n=@
  56. :: Cast the gate to the boolean type OR a cell that contains a tuple of atoms and the boolean type
  57. ::
  58. ^- ?(? [[@ @] ?])
  59. :: Evaluate to false if 'n' is < 4 or 'n' is odd
  60. ::
  61. ?: |((lth n 4) =((mod n 2) 1)) |
  62. :: Store the value 2 as an atom with face 'i'
  63. ::
  64. =/ i=@ 2
  65. :: Store the value of 'n' minus 2 as an atom with face 'j'
  66. ::
  67. =/ j=@ (sub n 2)
  68. :: Create a trap that casts to the boolean type OR a cell that contains a tuple of atoms and the boolean type
  69. ::
  70. |- ^- ?(? [[@ @] ?])
  71. :: Check if 'i' and 'j' are prime, and if they are, create a cell with 'i' and 'j' as a tuple and false.
  72. ::
  73. ?: &((prime i) (prime j)) [[i j] |]
  74. :: Otherwise, check if 'i' plus 2 equals 'n'
  75. ::
  76. ?: =((add 2 i) n)
  77. :: If it is, recurse back to the trap with 'i' incremented by 1, and 'j' decremented by 1
  78. ::
  79. $(i +(i), j (dec j))
  80. :: Note the end of the core's arm defintions
  81. ::
  82. --
  83.  
  84.  
  85.  
  86.  
  87. PART 2
  88.  
  89. :: the comment ":: code belongs here" indicates that one or more lines of code are needed to make this section of the program work.
  90. !:
  91. |= raw=tape
  92. =<
  93. :: code belongs here
  94. (convert (cuss raw))
  95. |%
  96. ++ convert
  97. :: code belongs here
  98. |= input=tape
  99. :: (~(got by a) b) produces the value located at key b within map a
  100. =/ chart ~(got by table)
  101. =/ output=tape ""
  102. |- ^- tape
  103. :: code belongs here
  104. ?~ input output
  105. ?: (~(has by table) i.input)
  106. =/ token=cord (chart i.input)
  107. =/ suffix=tape (trip token)
  108. $(output (weld output suffix), input t.input)
  109. $(input t.input)
  110.  
  111. ++ table
  112. %- my
  113. :~ :- 'A' '.-'
  114. :- 'B' '-...'
  115. :- 'C' '-.-.'
  116. :- 'D' '-..'
  117. :- 'E' '.'
  118. :- 'F' '..-.'
  119. :- 'G' '--.'
  120. :- 'H' '....'
  121. :- 'I' '..'
  122. :- 'J' '.---'
  123. :- 'K' '-.-'
  124. :- 'L' '.-..'
  125. :- 'M' '--'
  126. :- 'N' '-.'
  127. :- 'O' '---'
  128. :- 'P' '.--.'
  129. :- 'Q' '--.-'
  130. :- 'R' '.-.'
  131. :- 'S' '...'
  132. :- 'T' '-'
  133. :- 'U' '..-'
  134. :- 'V' '...-'
  135. :- 'W' '.--'
  136. :- 'X' '-..-'
  137. :- 'Y' '-.--'
  138. :- 'Z' '--..'
  139. :- '0' '-----'
  140. :- '1' '.----'
  141. :- '2' '..---'
  142. :- '3' '...--'
  143. :- '4' '....-'
  144. :- '5' '.....'
  145. :- '6' '-....'
  146. :- '7' '--...'
  147. :- '8' '---..'
  148. :- '9' '----.'
  149. :- ' ' ' '
  150. ==
  151. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement