Training the AI to write better LiveCode
Mark Smith
marksmithhfx at gmail.com
Sat Jan 14 14:13:15 EST 2023
We live in interesting times.
> On 14 Jan 2023, at 3:22 am, Geoff Canyon via use-livecode <use-livecode at lists.runrev.com> wrote:
>
> So just now I put this in to chatgpt:
>
> "fizz bin" is a program that counts from 1 to 100, replacing every number
> divisible by 5 with "fizz" and every number divisible by 7 with "bin"; and
> numbers divisible by both 5 and 7 with "fizz bin". Write code in the
> programming language livecode to execute "fizz bin"
>
> This is what ChatGPT responded with:
>
> repeat with x = 1 to 100
> if x mod 5 = 0 and x mod 7 = 0 then
> put "fizz bin"
> else if x mod 5 = 0 then
> put "fizz"
> else if x mod 7 = 0 then
> put "bin"
> else
> put x
> end if
> end repeat
>
> That works as-is, but obviously the results fly by in the message box. So I
> put a field and a button on a card, and edited to this, and it works
> perfectly:
>
> on mouseUp
> repeat with x = 1 to 100
> if x mod 5 = 0 and x mod 7 = 0 then
> put "fizz bin" & cr after fld 1
> else if x mod 5 = 0 then
> put "fizz" & cr after fld 1
> else if x mod 7 = 0 then
> put "bin" & cr after fld 1
> else
> put x & cr after fld 1
> end if
> end repeat
> end mouseUp
>
> Then as I was typing this email I realized I was potentially selling
> chatgpt short, so I put this in:
>
> Modify that code so it works as an "on mouseUp" handler in a button, and
> concatenates the results on individual lines, and puts the final result
> into the field "display"
>
> And it output this:
>
>
> on mouseUp
> put "" into fizzbin
> repeat with x = 1 to 100
> if x mod 5 = 0 and x mod 7 = 0 then
> put "fizz bin" & return after fizzbin
> else if x mod 5 = 0 then
> put "fizz" & return after fizzbin
> else if x mod 7 = 0 then
> put "bin" & return after fizzbin
> else
> put x & return after fizzbin
> end if
> end repeat
> put fizzbin into field "display"
> end mouseUp
>
> And it followed that with instructions:
>
> This code uses the "on mouseUp" handler to execute the script when the
> button is clicked. The results are concatenated and separated by a new line
> using the "& return" operator after each output. The final result is placed
> into a field with the name "display" using the "put" command.
>
> I'll save you the trouble: that code executes perfectly right out of the
> box, with no errors, and of course even though I didn't ask it to, it uses
> a variable to store the intermediate values, saving the field update for
> the end.
>
> If I were writing it, I'd probably do something like
>
> on mouseUp
> put "" into fizzbin
> repeat with x = 1 to 100
> if x mod 5 = 0 then put "fizz " after fizzbin
> if x mod 7 = 0 then put "bin " after fizzbin
> if char -1 of fizzbin is space
> then put return into char -1 of fizzbin
> else put x & return after fizzbin
> end repeat
> put fizzbin into field "display"
> end mouseUp
>
> That might be a bit faster, but probably not as clear.
>
> Finally, I gave it the input:
>
> Now rewrite the code to be a function named "fizzbin" that takes an
> argument "n", repeats with 1 to n, and returns the results.
>
> function fizzbin n
> put "" into fizzbin
> repeat with x = 1 to n
> if x mod 5 = 0 and x mod 7 = 0 then
> put "fizz bin" & return after fizzbin
> else if x mod 5 = 0 then
> put "fizz" & return after fizzbin
> else if x mod 7 = 0 then
> put "bin" & return after fizzbin
> else
> put x & return after fizzbin
> end if
> end repeat
> return fizzbin
> end fizzbin
>
> This works perfectly, and chat gpt also documents its answer:
>
> This code defines a function named "fizzbin" which takes an argument "n"
> that determines the range of the repeat loop. The function builds the
> result string in a variable "fizzbin" and return the final result. The
> calling script can then use the returned value for further processing or
> for displaying in a field.
>
> Grammatical issue notwithstanding, that's good documentation.
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list