Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
|
|
@ -15,4 +15,4 @@ Y
|
|||
set :fibo
|
||||
|
||||
for j range 0 10:
|
||||
print fibo j
|
||||
!print fibo j
|
||||
|
|
|
|||
|
|
@ -12,4 +12,4 @@ fibo-2 n:
|
|||
call
|
||||
|
||||
for j range 0 10:
|
||||
print fibo-2 j
|
||||
!print fibo-2 j
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
].
|
||||
|
|
|
|||
3
Task/Anonymous-recursion/Forth/anonymous-recursion-3.fth
Normal file
3
Task/Anonymous-recursion/Forth/anonymous-recursion-3.fth
Normal 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 . ;
|
||||
7
Task/Anonymous-recursion/Julia/anonymous-recursion.julia
Normal file
7
Task/Anonymous-recursion/Julia/anonymous-recursion.julia
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue