ChatGPT, AI language model makes a conversation, not only chat but also presents programming codes. It already knows Mathematica, and can produce the codes, however, checking the coding, we will soon understand the produced code is not a real code, saying, is the pseudo code.
So, produced pseudo codes have been meaningless? No, ChatGPT ability is fantastic one, and we should not abort without some considering to use.
I'd like to present some useful examples of the ChatGPT pseudocode, and working codes modified from the pseudo codes. AI can automatically produced, and plentiful, pseudo codes bunch as the samples. These characteristics may not be abandoned even if for Mathematica usage.
Pseudo sample 1
<< Developer`
Person::usage =
"Person[name_, age_] represents a person with the specified name \
and age.";
Person[name_, age_] :=
Module[{}, Person[name, age] = Inherit[{name, age}, Person]]
Person /: Greet[Person[name_]] :=
Module[{}, "Hello, my name is " <> name]
Person /: Birthday[Person[name_, age_]] :=
Module[{}, Person[name, age + 1]]
p1 = Person["Alice", 25]
Greet[p1]
p2 = Birthday[p1]
p2
Working sample 1
Person[name_, age_] := Module[{myage = age},
Greet[name] := "Hello, my name is " <> name;
Birthday[name] := {name, age + 1}
]
Person["Alice", 25]
Greet["Alice"]
Hello, my name is Alice
Birthday["Alice"]
{"Alice", 26}
Pseudo sample 2
<< Developer`
Rectangle::usage =
"Rectangle[width_, height_] represents a rectangle with the \
specified width and height.";
Rectangle[width_, height_] :=
Module[{},
Rectangle[width, height] = Inherit[{width, height}, Rectangle]]
Rectangle /: Area[Rectangle[width_, height_]] := width height
Rectangle /: Perimeter[Rectangle[width_, height_]] :=
2 (width + height)
Rectangle /: Draw[Rectangle[width_, height_], opts___] :=
Module[{},
Rectangle[width, height] =
Graphics[{Rectangle[{0, 0}, {width, height}]}, opts]]
r1 = Rectangle[10, 20]
Area[r1]
Perimeter[r1]
Draw[r1, Axes -> True]
Working sample 2
rectangle[name_ {width_, height_}] :=
Module[{myw = width, myh = height},
area[name] := myw*myh;
perimeter[name] := 2 (myw + myh)
]
rectangle[r1 {10, 20}]
area[r1]
200
perimeter[r1]
60
Pseudo sample 3
<< Developer`
Employee::usage =
"Employee[name_, salary_] represents an employee with the specified \
name and salary.";
Employee[name_, salary_] :=
Module[{}, Employee[name, salary] = Inherit[{name, salary}, Employee]]
Employee /: Raise[Employee[name_, salary_], amount_] :=
Module[{}, Employee[name, salary + amount]]
Employee /: Paycheck[Employee[name_, salary_]] :=
Module[{},
"Employee " <> name <> " was paid $" <> ToString[salary] <> "."]
e1 = Employee["Bob", 50000]
e2 = Raise[e1, 1000]
Paycheck[e2]
Working sample 3
Employee[name_, salary_] := Module[{mySalary = salary},
raise[name, amount_] := mySalary += amount;
paycheck[name] :=
"Employee " <> name <> " was paid " <> ToString[mySalary] <> "."
]
Employee["Bob", 50000]
raise["Bob", 1000]
51000
paycheck["Bob"]
"Employee Bob was paid 51000."
Enjoy, Mathematica OOP.