Chat-GPT is OpenAI’s latest release of it GPT series language models. Its capabilities have surpassed a threshold that made it famous outside of the IT crowd. In the mean time, it has gathered many supporters and possibly even more haters. Let me test its ability to generate structured text.
Prompt: write a function in structured text which replaces all vowels with the numbers 1-5.
FUNCTION ReplaceVowels(s: STRING) : STRING
VAR
i: INT;
vowels: ARRAY[1..5] OF CHAR;
BEGIN
vowels[1] := 'a';
vowels[2] := 'e';
vowels[3] := 'i';
vowels[4] := 'o';
vowels[5] := 'u';
FOR i := 1 TO LEN(s) DO
IF (s[i] = vowels[1]) THEN s[i] := '1';
IF (s[i] = vowels[2]) THEN s[i] := '2';
IF (s[i] = vowels[3]) THEN s[i] := '3';
IF (s[i] = vowels[4]) THEN s[i] := '4';
IF (s[i] = vowels[5]) THEN s[i] := '5';
END_FOR;
ReplaceVowels := s;
END_FUNCTION
This function takes a string as input, and replaces all vowels with the numbers 1-5. The function first declares an array of characters called “vowels” which contains all the vowels. Then it loops through each character of the input string, and checks if the current character is equal to any of the vowels in the array. If it is, it replaces that character with the corresponding number (1 for ‘a’, 2 for ‘e’, etc.). Finally, the function returns the modified string.
Pretty good! Not valid structured text, but it’s close. Lets try to call the function anyway, to see if it works.
Prompt: ReplaceVowels(“Apple”)
If the input string is “Apple” and you call the ReplaceVowels function with “Apple” as the argument, the function will loop through each character of the input string and check if it is a vowel. In this case, the vowels in the word “Apple” are ‘A’ and ‘e’, so the function will replace the ‘A’ with ‘1’ and ‘e’ with ‘2’. So the output will be “1ppl2”
It is quite verbose, but it gave the correct answer! Not bad. Although, it probably didn’t ‘use’ the function, instead it used the knowledge from the initial prompt what the function should do. Let’s optimize the function a little.
Prompt: modify the function such that you use a switch case instead of IF ELSE statements
FUNCTION ReplaceVowels(s: STRING) : STRING
VAR
i: INT;
BEGIN
FOR i := 1 TO LEN(s) DO
CASE s[i] OF
'a': s[i] := '1';
'e': s[i] := '2';
'i': s[i] := '3';
'o': s[i] := '4';
'u': s[i] := '5';
END_CASE;
END_FOR;
ReplaceVowels := s;
END_FUNCTION
This function uses a switch case statement instead of IF ELSE statements to check if a character is a vowel and replace it with the corresponding number. The switch case statement checks the value of the current character in the input string (s[i]), and replaces it with the corresponding number based on the case that matches the character.
Very nice. Next improvement.
Prompt: use a new string to save the modified string into
FUNCTION ReplaceVowels(s: STRING) : STRING
VAR
i: INT;
newStr: STRING;
BEGIN
newStr := '';
FOR i := 1 TO LEN(s) DO
CASE s[i] OF
'a': newStr := newStr + '1';
'e': newStr := newStr + '2';
'i': newStr := newStr + '3';
'o': newStr := newStr + '4';
'u': newStr := newStr + '5';
ELSE newStr := newStr + s[i];
END_CASE;
END_FOR;
ReplaceVowels := newStr;
END_FUNCTION
Cool.
Prompt: you can’t concatenate strings with a + in structured text. You need to use the function CONCAT(‘str1’, ‘ str2’ ); Change the function such that you use CONCAT.
FUNCTION ReplaceVowels(s: STRING) : STRING
VAR
i: INT;
newStr: STRING;
BEGIN
newStr := '';
FOR i := 1 TO LEN(s) DO
CASE s[i] OF
'a': newStr := CONCAT(newStr, '1');
'e': newStr := CONCAT(newStr, '2');
'i': newStr := CONCAT(newStr, '3');
'o': newStr := CONCAT(newStr, '4');
'u': newStr := CONCAT(newStr, '5');
ELSE newStr := CONCAT(newStr, s[i]);
END_CASE;
END_FOR;
ReplaceVowels := newStr;
END_FUNCTION
Impressive! now lets try to get and END_VAR
in there.
Prompt: you forgot the END_VAR
FUNCTION ReplaceVowels(s: STRING) : STRING
VAR
i: INT;
newStr: STRING;
BEGIN
newStr := '';
FOR i := 1 TO LEN(s) DO
CASE s[i] OF
'a': newStr := CONCAT(newStr, '1');
'e': newStr := CONCAT(newStr, '2');
'i': newStr := CONCAT(newStr, '3');
'o': newStr := CONCAT(newStr, '4');
'u': newStr := CONCAT(newStr, '5');
ELSE newStr := CONCAT(newStr, s[i]);
END_CASE;
END_FOR;
ReplaceVowels := newStr;
END_FUNCTION
END_VAR
Uh-oh. This is where it starts to fail. Maybe If I’m more explicit.
Prompt: the end_var should come before BEGIN. Also with twincat structed text, the input variables need to be placed between VAR INPUT and END_VAR, not in brackets after the function name,
VAR_INPUT
s: STRING;
END_VAR
VAR
i: INT;
newStr: STRING;
FUNCTION ReplaceVowels() : STRING
BEGIN
newStr := '';
FOR i := 1 TO LEN(s) DO
CASE s[i] OF
'a': newStr := CONCAT(newStr, '1');
'e': newStr := CONCAT(newStr, '2');
'i': newStr := CONCAT(newStr, '3');
'o': newStr := CONCAT(newStr, '4');
'u': newStr := CONCAT(newStr, '5');
ELSE newStr := CONCAT(newStr, s[i]);
END_CASE;
END_FOR;
ReplaceVowels := newStr;
END_FUNCTION
It is becoming a hot mess. It forgot one END_VAR
and moved the FUNCTION too far down. One more attempt:
Prompt: The FUNCTION declaration should go at the top. Followed by all VAR* blocks. Each VAR* block should be closed with END_VAR. And remove the parentheses after function name
Too many requests in 1 hour. Try again later.
¯\_(ツ)_/¯
I guess it’s time to call it a day. Quite impressive none-the-less what it showed there. It got 90% right, and might save you some typing and thinking. What’s your experience?