## Seven Languages in Seven Weeks Part 3: Fibonacci in Io

*by Justin Michalicek on April 15, 2011, 7:42 a.m. UTC*

I've started the chapter on the Io language. This language is pretty different from anything I've worked with. The syntax is a bit strange at times, but it seems to be very flexible. Io is a prototype language, so every object is a clone of another object starting with the base Object object that is the root of all objects.

I haven't done all of the exercises yet, but I did solve the Fibonacci problem for Day 2 of Io. In this exercise the goal is to create a method which you pass a number into. The method will then print out the value that is at that step of the fibonacci sequence.

With Fibonacci sequences, each number is the sum of the two previous, starting with a pair of 1s. So the sequence written out looks like 1,1,2,3,5,8,13,21,34 and so on. So calling fib(3) prints 2, and fib(9) prints 34.

As a bonus challenge, the book requests that you solve it with both a loop and a recursive method. Here are my solutions for both of them.

fib := method(target, current := 1 previous := 0 next_val := 1 position := 0 while(position < target, next_val := current + previous; previous := current; current := next_val; position := position + 1) previous println ) fib_recursive := method(target,iteration,previous,current, if(iteration == nil, iteration := 0) if(previous == nil, previous := 0) if(current == nil, current := 1) iteration := iteration + 1 next := current + previous if(iteration < target, fib_recursive(target,iteration, current, current + previous), current println ) )

I got bored and did a better recursive version that I think is more clear. Here it is.