請 AI 寫押韻的程式教學文章

彼得潘的 iOS App Neverland
2 min readMar 28, 2023

聽說 AI 除了會寫程式,它也是個浪漫的詩人。網路上有許多 GPT 4 寫押韻文章的有趣例子。

Now summarize this problem into a rhyming poem
with every line that rhymes

以下彼得潘請 GPT 4 用英文寫篇句句押韻的程式教學文。在程式學習上,閱讀朗朗上口的押韻文有許多好處,它會變得更有趣,不只能提升讀者的興趣,而且也會更容易記憶。

ps: 本來想請它寫中文,但測試後發現 AI 的中文押韻不太行,期待 GPT 5 成為會寫中文詩的詩人。

用英文介紹 swift computed property ,每一句都要押韻

In Swift there’s a feature, quite grand,
Computed properties at your command.

Unlike stored props, they don’t hold a value,
Instead, they calculate, as logic they channel.

When compared to stored properties, do note,
Differences emerge, with examples to quote.

Stored properties keep values in memory,
Computed ones perform math, for all to see.

To use them in Swift, pay close attention,
A code example here, for your comprehension.

class Rectangle {
var width: Double
var height: Double

var area: Double {
return width * height
}

init(width: Double, height: Double) {
self.width = width
self.height = height
}
}

In this example, a class we create,
With computed prop area, its use we demonstrate.

After the class is crafted, do not forget,
To instantiate an object, with a simple set.

let rectangle = Rectangle(width: 3, height: 4)
print("Area of the rectangle: \(rectangle.area)") // Outputs "Area of the rectangle: 12.0"

Now that we’ve seen how computed props behave,
Let’s take another step, more knowledge to save.

Not only for reading, but writing as well,
Getters and setters, in computed props dwell.

With these in place, you’ll soon understand,
Customizing behaviors goes just as planned.

To see this in action, let’s modify our code,
A new example now, our knowledge to goad.

class Circle {
var radius: Double

var diameter: Double {
get {
return radius * 2
}
set {
radius = newValue / 2
}
}

init(radius: Double) {
self.radius = radius
}
}

In this Circle class, a new computed prop,
Diameter we calculate, with radius to adopt.

Getters and setters, as you can see,
Allow flexibility, for you and for me.

To put this to test, let’s create a circle,
And modify diameter, with a code that’s quite simple.

let circle = Circle(radius: 5)
print("Diameter of the circle: \(circle.diameter)") // Outputs "Diameter of the circle: 10.0"
circle.diameter = 20
print("New radius of the circle: \(circle.radius)") // Outputs "New radius of the circle: 10.0"

Now you have seen, how computed props shine,
In Swift programming, a feature divine.

--

--

彼得潘的 iOS App Neverland

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com