September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,28 +1,37 @@
|
|||
import extensions.
|
||||
import extensions;
|
||||
|
||||
fib(n)
|
||||
[
|
||||
{
|
||||
if (n < 0)
|
||||
[ InvalidArgumentException new:"Must be non negative"; raise ].
|
||||
{ InvalidArgumentException.raise() };
|
||||
|
||||
^ (:n)
|
||||
[
|
||||
if (n > 1)
|
||||
[ ^ @self(n - 2) + @self(n - 1) ];[ ^ n ]
|
||||
](n)
|
||||
]
|
||||
|
||||
public program
|
||||
[
|
||||
-1 to:10 do(:i)
|
||||
[
|
||||
try (console printLine("fib(",i,")=",fib(i)))
|
||||
^ (n)
|
||||
{
|
||||
on(Exception e)
|
||||
[
|
||||
console printLine:"invalid"
|
||||
]
|
||||
if (n > 1)
|
||||
{
|
||||
^ this self(n - 2) + (this self(n - 1))
|
||||
}
|
||||
else
|
||||
{
|
||||
^ n
|
||||
}
|
||||
}(n)
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
for (int i := -1, i <= 10, i += 1)
|
||||
{
|
||||
console.print("fib(",i,")=");
|
||||
try
|
||||
{
|
||||
console.printLine(fib(i))
|
||||
}
|
||||
].
|
||||
console readChar
|
||||
]
|
||||
catch(Exception e)
|
||||
{
|
||||
console.printLine:"invalid"
|
||||
}
|
||||
};
|
||||
|
||||
console.readChar()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,3 +15,16 @@
|
|||
{fibo 1000} -> 7.0330367711422765e+208
|
||||
{map fibo {serie 1 20}}
|
||||
-> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
|
||||
|
||||
We could also avoid any name and write an IIFE
|
||||
|
||||
{{lambda {:n}
|
||||
{{lambda {:f :n :a :b} {:f :f :n :a :b}}
|
||||
{lambda {:f :n :a :b}
|
||||
{if {< :n 0}
|
||||
then the number must be positive!
|
||||
else {if {< :n 1}
|
||||
then :a
|
||||
else {:f :f {- :n 1} {+ :a :b} :a}}}} :n 1 0}}
|
||||
8}
|
||||
-> 34
|
||||
|
|
|
|||
10
Task/Anonymous-recursion/Ol/anonymous-recursion.ol
Normal file
10
Task/Anonymous-recursion/Ol/anonymous-recursion.ol
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(define (fibonacci n)
|
||||
(if (> 0 n)
|
||||
"error: negative argument."
|
||||
(let loop ((a 1) (b 0) (count n))
|
||||
(if (= count 0)
|
||||
b
|
||||
(loop (+ a b) a (- count 1))))))
|
||||
|
||||
(print
|
||||
(map fibonacci '(1 2 3 4 5 6 7 8 9 10)))
|
||||
43
Task/Anonymous-recursion/Rust/anonymous-recursion.rust
Normal file
43
Task/Anonymous-recursion/Rust/anonymous-recursion.rust
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
fn fib(n: i64) -> Option<i64> {
|
||||
// A function declared inside another function does not pollute the outer namespace.
|
||||
fn actual_fib(n: i64) -> i64 {
|
||||
if n < 2 {
|
||||
n
|
||||
} else {
|
||||
actual_fib(n - 1) + actual_fib(n - 2)
|
||||
}
|
||||
}
|
||||
|
||||
if n < 0 {
|
||||
None
|
||||
} else {
|
||||
Some(actual_fib(n))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Fib(-1) = {:?}", fib(-1));
|
||||
println!("Fib(0) = {:?}", fib(0));
|
||||
println!("Fib(1) = {:?}", fib(1));
|
||||
println!("Fib(2) = {:?}", fib(2));
|
||||
println!("Fib(3) = {:?}", fib(3));
|
||||
println!("Fib(4) = {:?}", fib(4));
|
||||
println!("Fib(5) = {:?}", fib(5));
|
||||
println!("Fib(10) = {:?}", fib(10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fib() {
|
||||
assert_eq!(fib(0).unwrap(), 0);
|
||||
assert_eq!(fib(1).unwrap(), 1);
|
||||
assert_eq!(fib(2).unwrap(), 1);
|
||||
assert_eq!(fib(3).unwrap(), 2);
|
||||
assert_eq!(fib(4).unwrap(), 3);
|
||||
assert_eq!(fib(5).unwrap(), 5);
|
||||
assert_eq!(fib(10).unwrap(), 55);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_argument() {
|
||||
assert_eq!(fib(-1), None);
|
||||
}
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
{ |i|
|
||||
func fib(n) {
|
||||
return NaN if (n < 0)
|
||||
|
||||
func (n) {
|
||||
if (n < 0) { return NaN }
|
||||
n < 2 ? n
|
||||
: (__FUNC__(n-2) + __FUNC__(n-1))
|
||||
}(i).say
|
||||
} * 10
|
||||
: (__FUNC__(n-1) + __FUNC__(n-2))
|
||||
}(n)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
{ |i|
|
||||
{ |n|
|
||||
if (n < 0) { return NaN }
|
||||
func fib(n) {
|
||||
return NaN if (n < 0)
|
||||
|
||||
{|n|
|
||||
n < 2 ? n
|
||||
: (__BLOCK__(n-2) + __BLOCK__(n-1))
|
||||
}(i).say
|
||||
} * 10
|
||||
: (__BLOCK__(n-1) + __BLOCK__(n-2))
|
||||
}(n)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue