2014年7月31日木曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 5(Making Choices)、5.6(Exercises) 6.をSwiftで考えてみる。

5.6(Exercises) 6.

コード(Xcode)

section-1.swift

// Playground - noun: a place where people can play

func different(a:Int, b:Int) -> Bool {
    return a == b;
}
func different(a:String, b:String) -> Bool {
    return a == b;
}

var nums = 1...5;
for a in nums {
    for b in nums {
        println("\(a) == \(b): \(different(a, b))");
    }
}

var strs = ["a", "b", "c", "d", "e"];
for a in strs {
    for b in strs {
        println("\(a) == \(b): \(different(a, b))")
    }
}

入出力結果(Console Output, Assistant Editor)

1 == 1: true
1 == 2: false
1 == 3: false
1 == 4: false
1 == 5: false
2 == 1: false
2 == 2: true
2 == 3: false
2 == 4: false
2 == 5: false
3 == 1: false
3 == 2: false
3 == 3: true
3 == 4: false
3 == 5: false
4 == 1: false
4 == 2: false
4 == 3: false
4 == 4: true
4 == 5: false
5 == 1: false
5 == 2: false
5 == 3: false
5 == 4: false
5 == 5: true
a == a: true
a == b: false
a == c: false
a == d: false
a == e: false
b == a: false
b == b: true
b == c: false
b == d: false
b == e: false
c == a: false
c == b: false
c == c: true
c == d: false
c == e: false
d == a: false
d == b: false
d == c: false
d == d: true
d == e: false
e == a: false
e == b: false
e == c: false
e == d: false
e == e: true

0 コメント:

コメントを投稿