Last active
December 16, 2015 18:58
-
-
Save wwqrd/5481292 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks - Io. fib.io: Fibonacci number, using recursion and loops; divide_by_zero.io: Change built-in behavior so that divide by zero returns 0. sum_matrix.io: Sum values in a two-dimensional list; my_average.io: Implement slot method myAverage on list primitive which returns the average of a list and throws an exception i…
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
oldDivision := Number getSlot("/") | |
Number / := method( a, | |
if( a <= 0, 0, self oldDivision( a ) ) | |
) | |
writeln( 1 / 2 ) # prints "0.5" | |
writeln( 1 / 1 ) # prints "1" | |
writeln( 1 / 0 ) # prints "0" |
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
# implemented with recursion | |
fib := method( i, | |
if(i > 2, fib(i-2) + fib(i-1), 1) | |
) | |
# implemented with loops | |
fib2 := method( i, | |
a := 1; b := 1; c := 0; j := 1; | |
while(j < i, | |
c = a + b; a = b; b = c; j = j + 1; | |
); | |
a | |
) | |
fib(6) println # prints "8" | |
fib2(7) println # prints "13" |
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
List myAverage := method( | |
if( | |
self size == 0, | |
Exception raise("The list is empty!"), | |
total := 0; | |
self foreach(i, v, | |
total = total + v | |
); | |
total / self size | |
) | |
) | |
newList := list(1,2,3,4,5,6,7) | |
newList myAverage println | |
newList2 := list() | |
newList2 myAverage println |
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
sumMatrix := method( list, | |
total := 0; | |
list foreach( i, v, | |
v foreach( j, w, | |
w println; | |
total = total + w | |
) | |
) | |
) | |
dummy := list( | |
list(1,2,3), | |
list(1,2,3), | |
list(1,2,3) | |
) | |
sumMatrix( dummy ) println |
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
Trix := Object clone | |
Trix dim := method( x, y, | |
self thislist := list() | |
for(i, 1, y, | |
self thislist append( | |
subList := list(); | |
for(j, 1, x, | |
subList append(nil) | |
); | |
subList | |
) | |
) | |
) | |
Trix set := method( x, y, val, | |
yList := thislist at(y - 1); | |
yList atPut(x - 1, val) | |
) | |
Trix get := method( x, y, val, | |
yList := thislist at(y - 1); | |
yList at(x - 1) | |
) | |
Trix println := method( | |
thislist println | |
) | |
trix := Trix clone | |
trix dim(3,4) | |
trix println | |
trix set(2, 1, 7) | |
trix set(1, 3, 5) | |
trix println | |
trix get(1, 3) println |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment