Jetzt aber. Nachdem ich letzte Woche ein paar kleinere Probleme mit Io hatte, kommt jetzt die Fortsetzung.

#!/usr/bin/io
# Autor: Jan Seeger
# Mail: jan@alphadev.net
# Web: jan.alphadev.net
#
# fibonacci.io
# Task of 5th Day, Seven Languages in Seven Weeks

recursive := Object clone
looped := Object clone

recursive fib := method(nth, return if(nth < 3, 1, fib(nth-2)+fib(nth-1) ))
looped fib := method(nth,
  one := 1
  two := 1
  for(i, 3, nth,
    tmp := two +one;
    one = two;
    two = tmp;
  )
  return two
)

recursive fib(10) println
looped fib(10) println
#!/usr/bin/io
# Autor: Jan Seeger
# Mail: jan@alphadev.net
# Web: jan.alphadev.net
#
# division_by_zero.io
# Task of 5th Day, Seven Languages in Seven Weeks

(100/5) println
(100/(-5)) println
(100/0) println

"redeclaring /" println
div := Number getSlot("/")
Number / = method(arg, if(arg == 0,0,div(arg)))

(100/5) println
(100/(-5)) println
(100/0) println
#!/usr/bin/io
# Autor: Jan Seeger
# Mail: jan@alphadev.net
# Web: jan.alphadev.net
#
# sum2d.io
# Task of 5th Day, Seven Languages in Seven Weeks

sum2d := method(s2d,
  temp := 0
  s2d foreach(item,  temp = temp + item sum )
  return temp
)

list1 := list(1,2,3)
list2 := list(4,5,6)
list3 := list(7,8,9)

sum2d(list(list1, list2, list3)) print
#!/usr/bin/io
# Autor: Jan Seeger
# Mail: jan@alphadev.net
# Web: jan.alphadev.net
#
# my_average.io
# Task of 5th Day, Seven Languages in Seven Weeks

List myAverage := method(

  self foreach(item,
    if (item type != 0 type, Exception raise("contains Nan value!"))
  )

  if( self size > 0, return (self sum/self size), return nil)
)

list(1,2,3) myAverage println
list(4.5,5,4.75) myAverage println
list() myAverage println

e := try( list(1,2,"three", 4) myAverage println; "doesn't work" println )
e catch (Exception, "works" println)

Erste Ansätze von Tests vorhanden. Mal schauen ob es ein Framework gibt. Ich muss mir einmal IoUnit anschauen.

#!/usr/bin/io
# Autor: Jan Seeger
# Mail: jan@alphadev.net
# Web: jan.alphadev.net
#
# list2d.io
# Task of 5th Day, Seven Languages in Seven Weeks

List2D := Object clone
List2D dim := method(x, y,
  self x := x;
  self y := y;
  self row := List clone
  for(i, 0, x-1,
    col := List clone;
    for(j, 0, y-1, col push(nil) );
    self row push(col) 
  );
  return self
)
List2D set := method(x, y, value,
  col := row at(x);
  col atPut(y, value)
)
List2D get := method(x, y,
  col := row at(x);
  return col at(y)
)
List2D print := method(
  self row foreach (col,
     col foreach (cell,
      "  " print;
      cell print;
    )
    "\n" print
  )
)
List2D transpose := method(
  temp := List2D clone
  xt := self x;
  yt := self y;
  temp dim(yt, xt)
  for(i, 0, x-1,
    for(j, 0, y-1, ""
      bla := get(i,j);
      temp set(j,i, bla);
    )
  );
  return temp;
)

test := List2D clone
test dim(2, 3)
test set(0,2, "bla")
test set(0,0, 0)
test set(0,1, 0.3)
test set(1,1, "test 123")

#test print
test transpose print

Diese Klasse gefällt mir noch nicht ganz. Irgend etwas fehlt noch.

#!/usr/bin/io
# Autor: Jan Seeger
# Mail: jan@alphadev.net
# Web: jan.alphadev.net
#
# random_number.io
# Task of 5th Day, Seven Languages in Seven Weeks

Main := Object clone;
Main init := method(
  Main correct := Random value(1,100) floor;
  Main counter := 0;
  Main last := nil
)
Main guess := method(
  if(last == correct, return true)

  Main input := File standardInput readLine("guess: ") asNumber;
  act 
  Main last := input;
)
Main act := method( 
  if(last==nil, return;)
  if(input==correct, "correct" println; return true)
  diffThis := (correct - input) abs
  diffLast := (correct - last) abs
  if(diffThis < diffLast, "hotter" println, "colder" println)
)

Main init
10 repeat(Main guess)

Kein Kommentar zur letzten Aufgabe. Ist einfach nur ein Versuch gewesen.