七週七語言:Io Day 2

第二天

今天做這些花了幾乎一個下午……主要原因是要查iolanguage.org上的文檔,而且那個文檔不知道爲什麼不能搜索 :(
這“一天”的內容,關於Io的控制語句、文件IO、錯誤處理、列表都有涉及。

個人比較喜歡的是Io的控制語句,寫在一個()裏,沒有else,條件、語句都用一個comma隔開的感覺很不錯;
有點不理解的是關於Io的消息機制和反射,雖然書上的例子舉的比較清楚,可是還是覺得將其稱之爲“反射”比較不習慣;再者,關於原型和對象的關係,感覺就是類的繼承(?!)……好吧,學習一門語言的過程還是挺歡樂的~

  • 計算fibonacci數列

    #循環的方法
    array := List clone
    array := list(1, 1)
    #change the value of a to cal fib(a)
    a ::= 10
    for(i, 2, a, 1, array append(array at(i-1) + array at(i-2)) println)
    #Output
    list(1, 1, 2)
    list(1, 1, 2, 3)
    list(1, 1, 2, 3, 5)
    list(1, 1, 2, 3, 5, 8)
    list(1, 1, 2, 3, 5, 8, 13)
    list(1, 1, 2, 3, 5, 8, 13, 21)
    list(1, 1, 2, 3, 5, 8, 13, 21, 34)
    list(1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
    list(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
    ==> list(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
    #遞歸的方法
    fib := method( n,
            if(((n == 1) or (n == 2)),return(1))
            #loop until n == i
            reFib := method( n, i, n1, n2,
                if((n == i),return(n1 + n2))
                    return(reFib(n,(i+1),n2,(n2 + n1)))
            )
            #The first condition of reFib
            return(reFib( n, 3, 1, 1))
    )
    fib(1) println
    fib(2) println
    fib(10) println
    #Output
    1
    1
    55
            
  • 在分母爲0的情況下,如何讓運算符返回0?

      Number setSlot("division", Number getSlot("/"))
      
      Number setSlot("/",
         method( denominator,
               if((denominator == 0),return (0))
               return(self division( denominator))
            )
      )
      
      (1 / 0) println
      (2 / 1) println
      #Output
      0
      2
            
  • 對二維數組進行求和

    #Define a 2D array
    2d_array := list(
    		 list(1, 2, 3),
    		 list(4, 5, 6),
    		 list(7, 8, 9)
    )
    sum := 0
    #Nested foreach statement
    2d_array foreach(r, r foreach(v, (sum = sum + v)))
    "The sum of this 2D array is: " print
    sum println
    #Output
    The sum of this 2D array is: 45
            
  • 對列表增加一個名爲myAverage的槽,以計算列表中所有數字的平均值。如果列表沒有數字會發生什麼?

    List myAverage := method(
    	 sum := 0 
    	 self foreach(k, 
    	 	  if((k type != "Number"),
    		  		Exception raise("There is a NaN-value in the list, please check your list." ),
    		  sum =( sum + k))
    	)
    	return (sum/(self size))
    )
    
    list(1, 2, 3) myAverage println
    list(1, "a", 3) myAverage println
    #Output
    2
      Exception: There is a NaN-value in the list, please check your list.
      ---------
      Exception raise                      myAverage.io 5
      List myAverage                       myAverage.io 13
      CLI doFile                           Z_CLI.io 140
      CLI run                              IoState_runCLI() 1
            
  • 對二維列表寫一個原型。該原型的dim(x, y)方法可爲一個包含y個列表的列表分配內存,其中每個列表都有x個元素,set(x, y)方法可以設置列表中的值,get(x, y)方法可返回列表中的值。

    2DList := List clone
    #This method should be considered later
    2DList dim := method(x, y,
    	   if( (self proto type == "List"),
    	   	   return 2DList clone dim(x, y)
    	   )
    	   self setSize(x)
    	   for(i, 0, (x-1), 1,
    	   		  self atPut(i, (list() setSize(y)))
    	   )
    	   return self
    )
    #set the value at(x) at(y)
    2DList set := method(x, y, value,
    	   self at(x) atPut(y, value)
    	   return self
    )
    #get the value 
    2DList get := method(x, y,
    	   return (self at(x) at(y))
    )
    
    matrix := 2DList clone dim(3, 3)
    for(i, 0, 2, 1, for(n, 0, 2, 1, matrix set(i, n, i+n))) println
    matrix get(1, 1) println
    #Output
    list(list(0, 1, 2), list(1, 2, 3), list(2, 3, 4))
    2 
            

  • 把矩陣寫入文件,並從文件中讀取矩陣。

    #Define a matrix
    matrix := list(
    	   list(1, 2, 3),
    	   list(4, 5, 6),
    	   list(7, 8, 9)
    )
    #Create a new file named test.txt
    data := File open("test.txt")
    #Transform the matrix into a sequence and write it into the file
    data File write(matrix serialized)
    data close
    #Read solution 1: use File open(still a serialized file)
    readData := File open("test.txt")
    readData readLine println
    readData close
    #Read solution 2: use doFile
    readData2 := doFile("test.txt")
    readData2 println
    readData2 close
    #Output
    list(list(1, 2, 3);, list(4, 5, 6);, list(7, 8, 9););
    list(list(1, 2, 3), list(4, 5, 6), list(7, 8, 9))
            
  • 寫一個猜數字程序。

    #Guess number
    standardIO := File standardInput
    guess := nil
    counter := 0
    #Get a random number between 1~100
    answer := (Random value(99)+1) floor
    
    while( counter < 10,
    	   "Guess a number(1..100):" println
    	   guess := standardIO readLine() asNumber()
    	   if((guess == answer), 
    	   			 "You are right!" println;break,
    				 if((guess > answer), 
    				 		   "Too big" println, 
    				 		   "Too small" println)
    	   )
           counter = counter + 1
    )
    #Output
    Guess a number(1..100):
    50 
    Too small
    Guess a number(1..100):
    75
    Too small
    Guess a number(1..100):
    90
    Too small
    Guess a number(1..100):
    95
    Too big
    Guess a number(1..100):
    93
    Too big
    Guess a number(1..100):
    92
    You are right!
            
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章