Created
November 18, 2011 19:43
-
-
Save philosodad/1377552 to your computer and use it in GitHub Desktop.
Io Day Two
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fiboRecursive := method(nth_value, if(nth_value <= 2, 1, | |
(fiboRecursive(nth_value - 2) + fiboRecursive(nth_value - 1)) | |
)) | |
fiboIterative := method(nth_value, value_list := list(0,1,1); | |
for(i, 0, nth_value, | |
if (value_list at (i), value_list at (i), | |
value_list append (value_list at (i-1) + value_list at (i-2) ) ) ); | |
value_list at (nth_value) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I admit that this is clumsy as hell | |
Matrix := Object clone | |
Matrix init := method(self matrix := List clone) | |
Matrix columns := method (ydim, for (i, 1, ydim, matrix append(List clone))) | |
Matrix rows := method (xdim, matrix foreach(v, for(i,1,xdim,v append(0)))) | |
Matrix dim := method(x, y, self columns(y); self rows(x)) | |
Matrix get := method(x,y, matrix at(y) at (x)) | |
Matrix set := method(x,y, value, matrix at(y) atPut(x, value)) | |
Matrix numColumns := method(matrix size) | |
Matrix numRows := method(matrix at (0) size) | |
Matrix transpose := method( new_matrix := Matrix clone; new_matrix dim(self numColumns, self numRows); | |
for(i, 0, (new_matrix numRows - 1), | |
for(j, 0, (new_matrix numColumns -1), new_matrix set(i,j, self get(j,i))) | |
;new_matrix)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OperatorTable addOperator("$", 2) | |
Number $ := method(number, if(number==0, 0, self/number)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all of these will raise an exception if any item on the list is not a number | |
List addUp := method(the_sum := 0; self foreach(v, the_sum = the_sum + v); the_sum) | |
List addUpLispy := method(if (self first == nil, 0, self first + self rest addUpLispy) ) | |
//these will raise an exception if given an empty 2D array | |
sumArray := method(the_sum := 0; self foreach(v, the_sum = the_sum + v sum); the_sum) | |
sumArrayLispy := method(if(self first == nil, 0, self first sum + self rest sumArrayLispy)) | |
// this raises an exception if an item on the list is not a number | |
List myAverage := method(self addUp / self size) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment