Advertisement
Guest User

assignment-03.hoon

a guest
Feb 23rd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. :: Define a gate that takes one unsigned integer sample and produces the
  2. :: factorial of that integer: n * (n-1) * ... * 1.
  3. ::
  4. |= n=@ud
  5. :: Put the face `t` on an unsigned integer for the running product. Set
  6. :: its initial value to 1, the identity value for multiplication.
  7. ::
  8. =/ t=@ud 1
  9. :: Create a new gate to provide a convenient tail-recursion point. The
  10. :: recursion invariant is that `n` is the largest number not yet
  11. :: multiplied into the product, and `t` is the running product.
  12. ::
  13. |-
  14. :: Handle the termination condition of the recursion. If n is 1, the
  15. :: base case, ...
  16. ::
  17. ?: =(n 1)
  18. :: ... then we have finished multiplying all the values, and `t` is
  19. :: the final product.
  20. ::
  21. t
  22. :: Handle the recursive case. Multiply `n` into the running product,
  23. :: and decrement `n` by 1.
  24. ::
  25. $(n (dec n), t (mul t n))
  26.  
  27. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  28.  
  29. |= a=(list @)
  30. =/ i=@ud 0
  31. |-
  32. ?~ a
  33. !!
  34. ?: =(i 2)
  35. i.a
  36. $(a t.a, i +(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement