Instantly share code, notes, and snippets.

# jomontanari/1-fibonacci.io Created Nov 17, 2011

Seven Languages - IO Day 2
 loop_fib := method(limit, fib_sequence := list(1, 1) i := 2 while (i <= limit, i = i + 1; fib_sequence append(fib_sequence last + fib_sequence at((fib_sequence size) - 2))) fib_sequence at(limit-1) ) loop_fib(1) println loop_fib(4) println recurse_fib := method(limit, last_two_vals, if (limit == 0, last_two_vals last, limit -= 1 if(last_two_vals isNil, recurse_fib(limit, list(0, 1)), last_two_vals append(last_two_vals sum) recurse_fib(limit, (last_two_vals slice(1))) ) ) ) recurse_fib(1) println // should be 1 recurse_fib(4) println // should be 3 recurse_fib(6) println // should be 8
 (3/0) println // Infinity Number coreDivision := Number getSlot("/") 3 coreDivision(4) println // 0.75 Number / = method(denominator, if(denominator > 0, call target coreDivision(denominator), 0) ) (3/4) println // 0.75 (3/0) println // 0
 two_dimensional_array := list(list(1, 2), list(3, 4), list(5, 6)) two_dimensional_array map(x, x sum) sum println
 List myAverage := method( call target foreach(x, if(x type != "Number", Exception raise("List should be numeric"))) (call target sum) / (call target size) ) my_numeric_list := list(1, 2, 3) my_numeric_list myAverage println my_non_numeric_list := list(1, 2, "hello") my_non_numeric_list myAverage println
 TwoDimensionalList := Object clone TwoDimensionalList main_list := list() TwoDimensionalList dim := method(a,b, for(i, 1, a, child_list := list() for(j, 1, b, child_list append(nil)) main_list append(child_list) ) ) TwoDimensionalList set := method(x, y, value, main_list at(x) atPut(y, value) ) TwoDimensionalList get := method(x, y, main_list at(x) at(y) ) my_two_d_list := TwoDimensionalList clone my_two_d_list dim(3,4) my_two_d_list set(1, 1, "Hello") my_two_d_list set(1, 2, "World") my_two_d_list get(1, 1) println my_two_d_list get(1, 2) println
 TwoDimensionalList transpose := method( new_list := list() for(i, 0, main_list size, new_child_list := list() main_list foreach(x, new_child_list append (x at(i)) ) new_list append(new_child_list) ) main_list = new_list )
 matrix_file := File with("matrix.txt") matrix_file openForUpdating matrix_file write(my_two_d_list serialized()) matrix_file close reading_matrix := doFile("matrix.txt") reading_matrix println
 number_to_guess := Random value(0, 100) floor number_guessed := -1 last_number_guessed := -1 last_difference := -1 difference := -1 tries := 0 while (number_guessed != number_to_guess and tries < 10, last_number_guessed := number_guessed last_difference := difference writeln("Guess a number?") number_guessed := (File standardInput readLine asNumber) if (number_guessed == number_to_guess, break) if (number_guessed > number_to_guess, difference := number_guessed - number_to_guess, difference := number_to_guess - number_guessed) if (difference > last_difference and tries > 0, "colder!" println, if (tries > 0, "hotter!" println)) tries := tries + 1 ) if (number_guessed == number_to_guess, "You did it!" println, "Loser!" println)