Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -1,6 +1,6 @@
While implementing a recursive function, it often happens that we must resort to a separate "helper function" to handle the actual recursion.
This is usually the case when directly calling the current function would waste too many resources (stack space, execution time), cause unwanted side-effects, and/or the function doesn't have the right arguments and/and return values.
This is usually the case when directly calling the current function would waste too many resources (stack space, execution time), cause unwanted side-effects, and/or the function doesn't have the right arguments and/or return values.
So we end up inventing some silly name like "foo2" or "foo_helper". I have always found it painful to come up with a proper name, and see a quite some disadvantages:

View file

@ -15,4 +15,4 @@ Y
set :fibo
for j range 0 10:
print fibo j
!print fibo j

View file

@ -12,4 +12,4 @@ fibo-2 n:
call
for j range 0 10:
print fibo-2 j
!print fibo-2 j

View file

@ -1,22 +1,24 @@
#define system.
#symbol fibo = &&:n
#symbol fibo = (:n)
[
n < 0
? [ #throw InvalidArgumentException new:"Must be non negative". ].
^ &&:n [ (n > 1) ? [ ($self:(n - 2)) + ($self:(n - 1)) ] ! [ n ] ] : n.
^ { eval:n [ ^ (n > 1) ? [ ($self:(n - 2)) + ($self:(n - 1)) ] ! [ n ]. ] }:n.
].
#symbol program =
[
control from:-1 &to:10 &do: &&:i
control from:-1 &to:10 &do: i
[
console << "%fib(" << i << ")=".
console << "fib(" << i << ")=".
console writeLine:(fibo:i) | onInvalidArgumentError: &&:e
console writeLine:(fibo:i) | onInvalidArgumentError: e
[
console writeLine:"invalid".
].
].
console readChar.
].

View file

@ -0,0 +1,3 @@
: fib ( +n -- )
dup 0< abort" Negative numbers don't exist"
[: dup 2 < ?exit 1- dup MYSELF swap 1- MYSELF + ;] execute . ;

View file

@ -0,0 +1,7 @@
function fib(n)
if n < 0
throw(ArgumentError("negative arguments not allowed"))
end
aux(m) = m < 2 ? one(m) : aux(m-1) + aux(m-2)
aux(n)
end