第7回 練習問題のプログラム例

  1. 何を渡しても、”hello” と答える(値を返す)関数

     def hel(x:Any):String = { "hello" }
     // または
     def hel(x:Any) = "hello"
    

    関数の重畳使用

  2. 渡した数の3乗を計算する関数 cube

     def cube(n:BigInt) = n*n*n
    

    計算順序

  3. 2つの数の積を返す関数 mul

     def mul(n:BigInt,m:BigInt) = n*m
    


    再帰の動作

  4. n の m乗(べき乗)を計算する、再帰的な関数 pow を math.pow を使わずに、乗算の演算子だけで。

     def pow(n:BigInt,m:Int):BigInt =
         if(m<=0) 1
         else pow(n,m-1) * n  
    

補足

クラス階層