Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :: Define a gate that takes one unsigned integer sample and produces the
- :: factorial of that integer: n * (n-1) * ... * 1.
- ::
- |= n=@ud
- :: Put the face `t` on an unsigned integer for the running product. Set
- :: its initial value to 1, the identity value for multiplication.
- ::
- =/ t=@ud 1
- :: Create a new gate to provide a convenient tail-recursion point. The
- :: recursion invariant is that `n` is the largest number not yet
- :: multiplied into the product, and `t` is the running product.
- ::
- |-
- :: Handle the termination condition of the recursion. If n is 1, the
- :: base case, ...
- ::
- ?: =(n 1)
- :: ... then we have finished multiplying all the values, and `t` is
- :: the final product.
- ::
- t
- :: Handle the recursive case. Multiply `n` into the running product,
- :: and decrement `n` by 1.
- ::
- $(n (dec n), t (mul t n))
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- |= a=(list @)
- =/ i=@ud 0
- |-
- ?~ a
- !!
- ?: =(i 2)
- i.a
- $(a t.a, i +(i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement