ChatGPT has been a godsend for custom Mathematica functions, especially simple ones that may take 10–15 minutes to write but still take me out of the Thing I’m Actually Trying to Do.
Are they the most efficient functions there are? Probably not. But they are well-written, easy to debug, and let me put small flourishes that ultimately make my life easier but that I wouldn’t bother with if I had to do everything on my own.
A recent example is Mathemathica’s built-in Tally function which takes in a list and puts out the frequency of each element (e.g. {Yes, No, Yes, Yes}
in and {{Yes, 3}, {No, 1}}
out). I usually deal with longer lists where percentages would also come in handy, so having percentages also listed (i.e. {Yes, 3, 25}
) would be nice. I asked ChatGPT-4o, and what it gave was good enough:
This was, of course, only a part of the response. It tends to be professorial when answering code-related questions, which is very much appreciated.
TallyWithPercentages[list_] := Module[
{tallied, total},
tallied = Tally[list];
total = Length[list];
{#[[1]], {#[[2]], N[#[[2]]/total*100]}} & /@ tallied
]
It wasn’t exactly what I wanted because the output was {Yes, 3}, 25}
instead of {Yes, 3, 25}
, but that was easy to change with a simple intervention — Flatten
the output then re-Partition
it — without thinking too much about Mathemathica’s sometimes incomprehensible short-code which I forget after a few days of not needing it.
Could I have done this with a quick online search? Of course! But the cognitive load of doing it this way is essentially zero, as is the chance of getting distracted by something else happening on the page.