# Here's another iterative version of factorial, this one using continuations. # It starts by defining a 'decrement' function with an exceptional return. ( # Decrement the integer, or use the alternate return if it is 0: (\([alternate]) -> \n -> (continue <+ (n 0 equals) $ [done-> $ alternate [-> n 1 minus $ continue] [(done)] _select [\@s<+s] ] ) : ~True -> Integer -> Integer ) (\decrement -> # name for the above-defined function # Factorial of an integer: (\n -> # Use iterate_ to find the factorial of n. (&n&1&) # initial value (\([return]) -> \(&n&product&) -> (n ([->product$return]) decrement) (\next -> (& next & product n times &)) ) iterate_ (\@@i->i) ) # Name it 'factorial', and demonstrate. (\factorial -> {((5 factorial) show_integer) put; put_newline} ) ) )