-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,91 @@ | ||
# recursive | ||
|
||
[![Package Version](https://img.shields.io/hexpm/v/recursive)](https://hex.pm/packages/recursive) | ||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/recursive/) | ||
|
||
# recursive | ||
|
||
This library provides user/developer friendly Fixed-point combinator functions. | ||
|
||
```sh | ||
gleam add recursive | ||
``` | ||
|
||
## Recursive block | ||
|
||
```gleam | ||
import gleam/io | ||
import recursive | ||
pub fn main() { | ||
// Factorial | ||
io.debug({ | ||
use it, f <- recursive.use_fix(10) | ||
use it, f <- recursive.block(10) | ||
case it { | ||
0 -> 1 | ||
_ -> it * f(it - 1) | ||
} | ||
}) | ||
let fac = recursive.fix(fn (it, f) { | ||
case it { | ||
0 -> 1 | ||
_ -> it * f(it - 1) | ||
} | ||
}) | ||
// Tail recursive factorial | ||
io.debug({ | ||
use it, res, f <- recursive.use_fix2(10, 1) | ||
use it, res, f <- recursive.block2(10, 1) | ||
case it { | ||
0 -> res | ||
_ -> f(it - 1, res * it) | ||
} | ||
}) | ||
let memo_fac = recursive.fix2(fn (it, res, f) { | ||
case it { | ||
0 -> res | ||
_ -> f(it - 1, res * it) | ||
} | ||
}) | ||
// Tail recursive fibonacci | ||
io.debug({ | ||
use it, curr, prev, f <- recursive.use_fix3(9, 1, 0) | ||
use it, curr, prev, f <- recursive.block3(9, 1, 0) | ||
case it { | ||
0 -> curr | ||
_ -> f(it - 1, curr + prev, curr) | ||
} | ||
}) | ||
let fib = recursive.fix3(fn (it, curr, prev, f) { | ||
} | ||
``` | ||
|
||
## Recursive anonymous function | ||
|
||
```gleam | ||
import gleam/io | ||
import recursive | ||
pub fn main() { | ||
// Factorial | ||
let fac = recursive.func(fn (it, f) { | ||
case it { | ||
0 -> 1 | ||
_ -> it * f(it - 1) | ||
} | ||
}) | ||
io.debug(fac(10)) | ||
// Tail recursive factorial | ||
let memo_fac = recursive.func(fn (it, res, f) { | ||
case it { | ||
0 -> res | ||
_ -> f(it - 1, res * it) | ||
} | ||
}) | ||
io.debug(fac(10, 1)) | ||
// Tail recursive fibonacci | ||
let fib = recursive.func(fn (it, curr, prev, f) { | ||
case it { | ||
0 -> curr | ||
_ -> f(it - 1, curr + prev, curr) | ||
} | ||
}) | ||
io.debug(fac(9, 1, 0)) | ||
} | ||
``` | ||
|
||
Further documentation can be found at <https://hexdocs.pm/recursive>. | ||
|
||
## Development | ||
|
||
```sh | ||
gleam test | ||
``` | ||
|
||
--- | ||
Further documentation can be found at <https://hexdocs.pm/recursive>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters