#WEEKLY 17 (Fibonacci Numbers)
For this challenge, I immediately turned to metaprogramming. (Sure, you could use the direct mathematical formula, but where's the fun in that? :D) I also decided to do the challenge in Ruby since I haven't had much experience with it. I ended up with 3 different programs, all of which perform the task:
My first attempt worked, but was very boring:
This is extremely straightforward, and definitely meets the requirements, but I decided to try something more fun as well. I managed to make a really, really weird iteration device which is guaranteed to work (though it is certainly not efficient):
What on Earth?
I told you it was weird :) Essentially, this is how it works:
- We set up our constants, as per a standard iteration-based solution.
- We define a container class so that our exploit, which involves
method_missing, doesn't leak out into the global scope and cause weird stuff to happen.
- We then define
method_missingon this class — here we perform the basic fibonacci calculation. This is where the magic happens: we
self.senda message which is equal to the "next" method after the method that called
- Then we make a new instance of our container class. After, we define the "ending-point" method on that class so that we don't trying to find the next method on that class. This is calculated by eval'ing the base method (which is defined as
"a" * [term number]so that we don't wrap around, ie zz.next->aa)
.next'd as many times as we want to do the calculation.
- Finally, we send the "trigger" message, which is the base method as per the last step.
I was very happy when I got this working, but realized that the judges might consider this a form of recursion. So, consider my first solution as my "official" one.
Looking forward to how everyone decided to tackle this problem!
@JosephSanthosh My answer: No. I've never had an instance where I've had to use Ruby to do something, and since it hasn't increased my productivity I don't see why I'd ever chose to use it. I could be wrong, it just doesn't seem useful to me though. (Compare this to a functional language, Haskell, where I instantly noticed a productivity gain. I still like Haskell and chose to use it sometimes.)
@JosephSanthosh Haskell is a functional programming language. This means that:
1. No variables! Variables literally don't exist, so
a = 1; a = 2 is not possible.
2. Everything is done through the application of functions — there are no lists of statements. (You might think that Haskell's
do is a list of statements, but it's actually just a clever abstraction.)
3. This means that code has no side effects — i.e, a function which is given the same arguments will always return the same thing.
Why on earth would you want these constraints? Well, if a program written in a functional programming language compiles, 9 times out of 10 it will do exactly what you want. If you want to know more (I probably explained it bad, FP is a really good thing believe me) check out the website: https://haskell.org