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?

You

Write a Haskell conversion function from ISO 3166 codes to the corresponding country names.

ChatGPT

Here is one possible implementation of the conversion function:

typescript
import Data.Map (Map)
import qualified Data.Map as Map

type CountryCode = String
type CountryName = String

iso3166 :: Map CountryCode CountryName
iso3166 = Map.fromList [
("AF", "Afghanistan"),
("AX", "Åland Islands"),
("AL", "Albania"),
("DZ", "Algeria"),
("AS", "American Samoa"),
("AD", "Andorra"),
-- ... and so on, for all 249 entries in the ISO 3166 standard
]

toCountryName :: CountryCode -> Maybe CountryName
toCountryName code = Map.lookup code iso3166

The `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 of Maybe 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!


You

Make it total by using a sumtype for CountryCode.

ChatGPT

We can define a sumtype for CountryCode to make it total. Here is an example:

kotlin
data CountryCode = USA | CAN | MEX | BRA | ARG

data Phone = Phone CountryCode Int

data Person = Person {
name :: String,
phone :: Phone
}

Now, 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:

css
-- This will compile:
Person {
name: "John",
phone: Phone USA 1234567890
}

-- This will give a compile-time error:
Person {
name: "Maria",
phone: Phone UK 1234567890
}

Using 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.

You

No, I just need a conversion from ISO 3166 country codes to country names. I don't need any people or phone numbers.

ChatGPT

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.

You

But I want that as a Haskell conversion function, please.

ChatGPT

Here is the implementation of the conversion function in Haskell:

rust
binaryToDecimal :: String -> Int
binaryToDecimal = foldl (\acc x -> acc * 2 + digitToInt x) 0

This 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.