Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laurabeatris/list-length-recursion
Elixir algorithm exercise to practice recursion using tail call optimization
https://github.com/laurabeatris/list-length-recursion
algorithms-and-data-structures elixir functional-programming recursion
Last synced: 20 days ago
JSON representation
Elixir algorithm exercise to practice recursion using tail call optimization
- Host: GitHub
- URL: https://github.com/laurabeatris/list-length-recursion
- Owner: LauraBeatris
- Created: 2021-10-04T14:05:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-04T16:15:26.000Z (over 3 years ago)
- Last Synced: 2024-11-03T16:42:28.815Z (2 months ago)
- Topics: algorithms-and-data-structures, elixir, functional-programming, recursion
- Language: Elixir
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# List Length with Recursion
Elixir algorithm exercise to practice recursion using [tail call optimization](https://blog.appsignal.com/2019/03/19/elixir-alchemy-recursion.html)
## Interesting Resources
- [Iteration, Recursion, and Tail-call Optimization in Elixir
](https://blog.appsignal.com/2019/03/19/elixir-alchemy-recursion.html)
- [Multi-clause Functions with Pattern Matching and Guards in Elixir
](https://www.culttt.com/2016/05/23/multi-clause-functions-pattern-matching-guards-elixir)
- [Elixir Lists](https://inquisitivedeveloper.com/lwm-elixir-14/)## Module Usage
```elixir
ListLength.call([]) # Result: 0
ListLength.call([1, 2, 3]) # Result: 3
```## Algorithm Explanation
```elixir
def call(list), do: get_length(list, 0)# Hide the logic with private functions - helps to abstract the logic from the main function
# Multi-clause functions: overload a function by specifying different clauses to match against - if tail is empty, then return the current accumulator value
defp get_length([], acc), do: acc# Prepend to a list using the | operator in order to access head and tail - possible due to Elixir nature of having linked lists
# Recursively call function and increase accumulator argument until the tail is empty
defp get_length([_head | tail], acc), do: get_length(tail, 1 + acc)
```