Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
34
Task/Anonymous-recursion/Go/anonymous-recursion-2.go
Normal file
34
Task/Anonymous-recursion/Go/anonymous-recursion-2.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func fib(n int) (result int, err error) {
|
||||
var fib func(int) int // Must be declared first so it can be called in the closure
|
||||
fib = func(n int) int {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
return fib(n-1) + fib(n-2)
|
||||
}
|
||||
|
||||
if n < 0 {
|
||||
err = errors.New("negative n is forbidden")
|
||||
return
|
||||
}
|
||||
|
||||
result = fib(n)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
for i := -1; i <= 10; i++ {
|
||||
if result, err := fib(i); err != nil {
|
||||
fmt.Printf("fib(%d) returned error: %s\n", i, err)
|
||||
} else {
|
||||
fmt.Printf("fib(%d) = %d\n", i, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue