kensremit
I know elixir is a functional programming language and this means can’t make a change state in some problems? Do I am missing something?
Can use any techniques to replace for solution dynamic programming Fibonacci? This is my solution in Golang
package main
import "fmt"
func fib(n int) int {
// make initial array with size (n+1)
answer := make([]int, n+1)
// set answer[0] and answer[1] with default values
answer[0] = 0
answer[1] = 1
// run dynamic programming and memorize steps
// such that answer[i] = answer[i-1] + answer[i-2]
// for each 2 <= i <= n
for i := 2; i <= n; i++ {
answer[i] = answer[i-1] + answer[i-2]
}
// check answer array
fmt.Println(answer)
// fib(n) is last element of answer array
return answer[n]
}
func main() {
// test
fmt.Println(fib(10))
// output: [0 1 1 2 3 5 8 13 21 34 55]
// 55
}
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
chungwong
Do you mean this Elixir fibonacci #1 · GitHub
There is a tail call optimised version too.
kokolegorille
This is not possible, because there are no Arrays in Elixir, but linked List
In Elixir, You don’t access list element by index, because it’s expensive.
This is not possible, because i is mutated
Instead, You can have functions that transform input into output.
and if You want to cumulate, You can enumerate a range from 0 to 10 and pass the implementation details.
This is not efficient, as the result is recalculated. But You can make it better with recursion.
Exadra37
If you want to make the shift to functional programming then I strongly recommend that you first read this book:
This is the only book I know that spents a great deal of time educating you how to go from Object Orientated Programming and Procedural Programming to Functional Programming. @pragdave does an excellent job on making it click in our brains, can’t recommend it enough.
If you prefer video form, then he has a video course that does the same:
I have done both and that really helped me to let go away the old way of thinking about coding.
luizdamim
A solution using dynamic programming would be:
I added another solution using
Agenton the gist above (Elixir fibonacci #1 · GitHub).josevalim
You will often see fibonnaci written according to its functional definition but as others pointed out, it is not very efficient as you compute the whole chain on every operation:
However, you can write a looping version, as in Go in Elixir. In Elixir we don’t have traditional loops, instead we use recursion. The other insight is that, in order to compute fibbonacci, we don’t need the whole array of results, only the last two, so we can implement it like this:
It is worth reading more into recursion but one very useful exercise is to see how the execution goes manually. For example, for
fib(5), this is what happens:This is going to have the same perf complexity as the Go one.
This is an advanced topic, so please don’t consider it part of the answer, but there is another cool variant of the algorithm above which is to pack it into a stream. You can think of streams as lazy lists, which are generated on the fly, as needed! Here is a fib stream:
The cool think about the fib stream is that you are totally in control. If you want a single entry, you can have it. If you want a list, you got that too:
Stratus3D
I wrote two blog posts about Fibonacci algorithms in Elixir, posts include benchmarks of all the implementations I evaluated:
This pretty well sums up what I found when I benchmarked the implementations I looked at in those blog posts.