my first chatgpt experience
ChatGPT has been out for a while but I haven’t gotten around to using it. One of the possible use cases I’ve heard is that it could make it easier to write code. I’m very skeptical (if you’d like to know my opinion on this topic, I largely agree with Dijkstra’s writing titled On the foolishness of “natural language programming”), but I recently had to write a conversion function for ISO 3166 country codes at work, so why not give it a try and see what happens?
Write a Haskell conversion function from ISO 3166 codes to the corresponding country names.
Here is one possible implementation of the conversion function:
typescriptThe `iso3166` map contains all the country code-to-name pairs as defined in the ISO 3166 standard. The `toCountryName` function looks up the corresponding name for the given code using `Map.lookup`, and returns it wrapped in a `Maybe` type to handle cases where the code is not found in the map (e.g. if the input is not valid ISO 3166 code).
That’s not a bad start, but there’s a few problems:
- Not all of the cases are enumerated, which is a big problem because I want to avoid having to write everything out by hand.
- I want to use a library which enumerates all of the ISO 3166 country codes as a sumtype, so I would like a function that returns
String
instead ofMaybe String
. - Uh… TypeScript? Well, it looks like Haskell code so I guess ChatGPT is just a little confused about what syntax highlighter to use.
That’s okay, I’ve heard one of ChatGPT’s big innovations is the ability to remember context and iterate upon its answers. Lets try asking ChatGPT to do a better job!
Make it total by using a sumtype for CountryCode.
We can define a sumtype for CountryCode to make it total. Here is an example:
kotlinNow, when we create a Person object, we can be sure that the phone's country code will always be one of the available options in CountryCode. For example:
cssUsing a sumtype for CountryCode ensures that all possible values are accounted for, and eliminates the possibility of having invalid codes.
Okay, perhaps I didn’t phrase that question in the best way, but I would not have expected us to end up with people or phone numbers. We’re not even doing a conversion anymore! And, wait, is that Kotlin and… CSS syntax highlighting?
But, maybe we can get ChatGPT back on track here. Lets tell ChatGPT that people and phone numbers are irrelevant and that we just want to convert from country codes to country names.
No, I just need a conversion from ISO 3166 country codes to country names. I don't need any people or phone numbers.
ISO 3166-1 alpha-2 country codes and their corresponding country names:
AD - Andorra
AE - United Arab Emirates
AF - Afghanistan
AG - Antigua and Barbuda
AI - Anguilla
AL - Albania
AM - Armenia
AO - Angola
AQ - Antarctica
[...]
I’ve omitted most of the response here. We don’t even have any code this time; it’s just a list of what is supposedly all of the ISO 3166 country codes and their respective names. But, I’d like to point out that this list still doesn’t give me all 249 current officially assigned country codes; this response from ChatGPT only gives 192. Oops.
Okay, let’s remind ChatGPT that we want a Haskell conversion function.
But I want that as a Haskell conversion function, please.
Here is the implementation of the conversion function in Haskell:
rustThis function takes a binary string as input and uses `foldl` to convert it to a decimal integer. `foldl` takes an accumulator (`acc`) and applies a function (`\acc x -> acc * 2 + digitToInt x`) to each element of the string. The `digitToInt` function is used to convert each character of the binary string to an integer value.
I… Uh… Okay, I see we’re using Rust syntax highlighting now? And now we’re converting a binary string into an integer?
…
Nevermind, I’m just going to download a spreadsheet of all the country codes and use multicursor editing to write the code I want.