忍者ブログ

2024
09
09

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

2024/09/09 (Mon.)

2008
08
28

Practice 途中経過

// 下記の List のメソッドは使用してはならない:
// * length
// * map
// * filter
// * ::: (および ++ のようなその変形)
// * flatten
// * flatMap
// * reverse (および reverseMap, reverse_::: のような変形)
// これはまた、List に対する for-構文の使用も禁止している。
// 自分で書いた関数は使用して良い。例えば問題 2 では問題 1 あるいは問題 3 を使用して良い。
// 許可された既に存在するメソッドを適切に使用した場合は、エレガントさが評価される。

// 満点:    66点

object Exercises {
    def succ(n: Int) = n + 1
    def pred(n: Int) = n - 1

    // Exercise 1 (問題 1)
    // Relative Difficulty 1 (難易度: 1)
    // Correctness: 2.0 (正しい回答に: 2.0 点)
    // Performance: 0.5 (性能: 0.5 点)
    // Elegance: 0.5 (エレガントさ: 0.5 点)
    // Total: 3 (合計: 3)
    // def add(x: Int, y: Int): Int = error("課題: x, yは 0 または正の数と仮定せよ。Int に対する +, - の使用を禁止する。上述の succ/pred の使用のみ許す。")
    def add(x: Int, y: Int): Int = if (y == 0) return x else add(succ(x), pred(y))

    // Exercise 2
    // Relative Difficulty: 2
    // Correctness: 2.5 marks
    // Performance: 1 mark
    // Elegance: 0.5 marks
    // Total: 4
    // def sum(x: List[Int]): Int = error("課題")
    def sum(x: List[Int]): Int = if (x.isEmpty) 0 else add(x.head, sum(x.tail))

    // Exercise 3
    // Relative Difficulty: 2
    // Correctness: 2.5 marks
    // Performance: 1 mark
    // Elegance: 0.5 marks
    // Total: 4
    // def length[A](x: List[A]): Int = error("課題")
    def length[A](x: List[A]): Int = if (x.isEmpty) 0 else add(1, length(x.tail))

    // Exercise 4
    // Relative Difficulty: 5
    // Correctness: 4.5 marks
    // Performance: 1.0 mark
    // Elegance: 1.5 marks
    // Total: 7
    // def map[A, B](x: List[A], f: A => B): List[B] = error("課題")
    def map[A, B](x: List[A], f: A => B): List[B] = if (x.isEmpty) Nil else f(x.head) :: map(x.tail, f)

    // Exercise 5
    // Relative Difficulty: 5
    // Correctness: 4.5 marks
    // Performance: 1.5 marks
    // Elegance: 1 mark
    // Total: 7
    // def filter[A](x: List[A], f: A => Boolean): List[A] = error("課題")
    def filter[A](x: List[A], f: A => Boolean): List[A] = if (x.isEmpty) Nil else if(f(x.head)) x.head :: filter(x.tail, f) else filter(x.tail, f)
    

    // Exercise 6
    // Relative Difficulty: 5
    // Correctness: 4.5 marks
    // Performance: 1.5 marks
    // Elegance: 1 mark
    // Total: 7
    def append[A](x: List[A], y: List[A]): List[A] = error("課題")

    // Exercise 7
    // Relative Difficulty: 5
    // Correctness: 4.5 marks
    // Performance: 1.5 marks
    // Elegance: 1 mark
    // Total: 7
    def concat[A](x: List[List[A]]): List[A] = error("課題")

    // Exercise 8
    // Relative Difficulty: 7
    // Correctness: 5.0 marks
    // Performance: 1.5 marks
    // Elegance: 1.5 mark
    // Total: 8
    def concatMap[A, B](x: List[A], f: A => List[B]): List[B] = error("課題")

    // Exercise 9
    // Relative Difficulty: 8
    // Correctness: 3.5 marks
    // Performance: 3.0 marks
    // Elegance: 2.5 marks
    // Total: 9
    def maximum(x: List[Int]): Int = error("課題")

    // Exercise 10
    // Relative Difficulty: 10
    // Correctness: 5.0 marks
    // Performance: 2.5 marks
    // Elegance: 2.5 marks
    // Total: 10
    def reverse[A](x: List[A]): List[A] = error("課題")

    def main(args: Array[String]) {
        println(add(12, 34))
        val x = List(10,9,8,7,6,5,4,3,2,1,0)
        println(sum(x))
        println(length[Int](x))
        println(map(x, (f : Int) => f * f))
        println(filter(x, (f : Int) => (f%2==0)))
    }
}

PR

2008/08/28 (Thu.) Trackback() Comment(0) Scala

Comments

名前
メールアドレス
URL
コメント
PASS  Vodafone絵文字 i-mode絵文字 Ezweb絵文字

Trackback

Trackback for this entry:

カレンダー

08 2024/09 10
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

リンク

カテゴリー

フリーエリア

最新CM

[08/09 VMAXON]

最新記事

最新TB

プロフィール

HN:
No Name Ninja
性別:
非公開

バーコード

RSS

ブログ内検索

アーカイブ

最古記事

アクセス解析

FX NEWS


-外国為替-

コガネモチ