Skip to content

Latest commit

 

History

History
91 lines (75 loc) · 1.59 KB

README.md

File metadata and controls

91 lines (75 loc) · 1.59 KB

Package Version Hex Docs

recursive

This library provides user/developer friendly Fixed-point combinator functions.

gleam add recursive

Recursive block

import gleam/io
import recursive

pub fn main() {
   // Factorial
   io.debug({
    use it, f <- recursive.block(10)
    case it {
      0 -> 1
      _ -> it * f(it - 1)
    }
   })

   // Tail recursive factorial
   io.debug({
    use it, res, f <- recursive.block2(10, 1)
    case it {
      0 -> res
      _ -> f(it - 1, res * it)
    }
   })

   // Tail recursive fibonacci
   io.debug({
    use it, curr, prev, f <- recursive.block3(9, 1, 0)
    case it {
      0 -> curr
      _ -> f(it - 1, curr + prev, curr)
    }
   })
}

Recursive anonymous function

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.func2(fn (it, res, f) {
      case it {
        0 -> res
        _ -> f(it - 1, res * it)
      }
   })
   io.debug(fac(10, 1))

   // Tail recursive fibonacci
   let fib = recursive.func3(fn (it, curr, prev, f) {
      case it {
        0 -> curr
        _ -> f(it - 1, curr + prev, curr)
      }
   })
   io.debug(fac(9, 1, 0))
}

Development

gleam test

Further documentation can be found at https://hexdocs.pm/recursive.