mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
The rule changes are as follows: * All functions (introduced with the 'func' keyword) have argument labels for arguments beyond the first, by default. Methods are no longer special in this regard. * The presence of a default argument no longer implies an argument label. The actual changes to the parser and printer are fairly simple; the rest of the noise is updating the standard library, overlays, tests, etc. With the standard library, this change is intended to be API neutral: I've added/removed #'s and _'s as appropriate to keep the user interface the same. If we want to separately consider using argument labels for more free functions now that the defaults in the language have shifted, we can tackle that separately. Fixes rdar://problem/17218256. Swift SVN r27704
43 lines
1.5 KiB
Swift
43 lines
1.5 KiB
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
|
|
|
// From http://rosettacode.org/wiki/Factorial
|
|
func factorial(x: Int) -> Int {
|
|
if (x == 0) { return 1 }
|
|
return x * factorial(x-1)
|
|
}
|
|
|
|
// From http://rosettacode.org/wiki/Towers_of_Hanoi
|
|
func TowersOfHanoi(ndisks: Int, from: Int, to: Int, via: Int) -> Void {
|
|
if (ndisks == 1) {
|
|
print("Move disk from pole \(from) to pole \(to)\n")
|
|
}
|
|
else {
|
|
TowersOfHanoi(ndisks-1, from: from, to: via, via: to);
|
|
TowersOfHanoi(1, from: from, to: to, via: via);
|
|
TowersOfHanoi(ndisks-1, from: via, to: to, via: from);
|
|
}
|
|
}
|
|
|
|
// Driver code.
|
|
print("Factorial of 10 = \(factorial(10))\n\n")
|
|
print("Towers of Hanoi, 4 disks\n")
|
|
TowersOfHanoi(4, from: 1, to: 2, via: 3)
|
|
|
|
// CHECK: Factorial of 10 = 3628800
|
|
// CHECK: Towers of Hanoi, 4 disks
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 3
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 2
|
|
// CHECK-NEXT: Move disk from pole 3 to pole 2
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 3
|
|
// CHECK-NEXT: Move disk from pole 2 to pole 1
|
|
// CHECK-NEXT: Move disk from pole 2 to pole 3
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 3
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 2
|
|
// CHECK-NEXT: Move disk from pole 3 to pole 2
|
|
// CHECK-NEXT: Move disk from pole 3 to pole 1
|
|
// CHECK-NEXT: Move disk from pole 2 to pole 1
|
|
// CHECK-NEXT: Move disk from pole 3 to pole 2
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 3
|
|
// CHECK-NEXT: Move disk from pole 1 to pole 2
|
|
// CHECK-NEXT: Move disk from pole 3 to pole 2
|