Chat GPT from the Programming Perspective

Chat GPT from a programmer’s point-of-view.

In the November of 2022, OpenAI released their chatbot Chat GPT (Chat Generative Pre-Trained Transformer), and it has taken the world by storm ever since. Not only it can write stories, provide information (if it has the knowledge for it), generate templates for various things such as resumes and even ISO standards, but it can also generate code. and this article is all about the last thing, and its implications.

First, let’s try to understand intelligent computers through an example:

Look at this video. it is a compilation of the scenes of the Computer from Courage the Cowardly Dog, which proceeded the domination of search engine, and came in during the AI winter. (And yes, there is a reason why I capitalized c while spelling computer in its case.)

A sarcastic, condescending, sometimes trolling, and yet a helpful piece of device, Courage goes to this Computer whenever he has some problem which he cannot solve himself. Interestingly, it does behave like a chatbot, where Courage types out everything, and yet the computer replies vocally. When I first used Chat GPT, I was reminded of this sarcastic Computer with British accent. But these computers can be quite common in the fiction. Another toon from the 90s, called Dextor’s Laboratory also had a computer, which actually went beyond just answering question: it managed the entire lab, and all the experiments going on in that lab.

Please do remember these examples, as I will come back to them at the end of this article.

Some examples of Chat GPT’s capabilities:

Having taken its unwilling interview, I am familiar with Chat GPT’s story-generating capabilities. But now, I wanted to know about its coding capabilities. Normally, someone would have tried to ask it to solve some problem in a popular language like JavaScript, C++, or Java. But I wanted to throw a curveball at it, by asking about a niche language which is not used widely:

Do you know Common Lisp?

Here’s what I got in reply:

Chat GPT: Yes, I am familiar with Common Lisp. Common Lisp is a high-level, general-purpose programming language that was standardized in the 1980s and has a long history of use in a variety of fields, including artificial intelligence, finance, and engineering. It is known for its flexible and expressive syntax, as well as its support for object-oriented, functional, and imperative programming styles. Common Lisp is often used for rapid prototyping and development, as well as for applications that require high performance or extensive numerical or symbolic computing capabilities. It is an actively maintained, open-source language with a large and active community of users and developers.

The answer is a bit dry and formal, but for me, it was enough. So, I asked the next question:

Can you give two simple examples in Common Lisp?

Chat GPT: Certainly! Here are two simple examples in Common Lisp: Example 1: Hello, World! This is a classic example that simply prints "Hello, World!" to the screen:

(print "Hello, World!")

Example 2: Fibonacci Numbers This example defines a function that calculates the nth Fibonacci number:

(defun fib (n)
  (if (< n 2)
      n
      (+ (fib (- n 1)) (fib (- n 2)))))

To use this function, you can call it with a number as an argument, like this:

(fib 10)

This will return the 10th Fibonacci number, which is 55.

I deliberately asked for two examples, as I did not want it to just give me a hello world example, and just leave it at that. then I asked some more questions:

can you explain print function of Common Lisp?

Chat GPT: In Common Lisp, the print function is used to output a value to the standard output stream (e.g., the console). It takes a single argument, which can be any valid Lisp object, and prints a representation of that object to the output stream. For example, you can use print to output a string, like this:

(print "Hello, World!")

This will output the string "Hello, World!" to the console. You can also use print to output other types of values, such as numbers, lists, and symbols. For example:

(print 42)
(print '(1 2 3))
(print 'foo)

These examples will output the following values to the console:

42
(1 2 3)
FOO

By default, print adds a newline character after the output, so the output will appear on a separate line. You can suppress this behavior by setting the *print-newline* variable to nil before calling print. For example:

(let ((*print-newline* nil))
  (print "Hello, World!"))

This will output the string "Hello, World!" without a newline character after it.

I was honestly blown away by this example. It felt like I was reading a book, and this machine gave me an answer write out of it. which it probably did, come to think of it. I’m well aware that some Lisp graybeards would thump me in the comments for this, but I like this way of asking specific things about the language, rather than dig through the standards, where everything seems to be linked, and my brain which is already prone to wiki-walking goes crazy as a result. Not to mention, sometimes the standard feels lacking in examples for an inexperienced user like me.

Then I decided to ask about another language which is not used widely, aside from certain circles:

Do you know OCaml?

Chat GPT: Yes, I am familiar with OCaml (pronounced "oh-kam-el"). OCaml is a general-purpose programming language that was designed to be expressive, efficient, and easy to learn. It is a statically typed, functional programming language that is widely used in industry and academia for a variety of applications, including compilers, operating systems, and static analysis tools. OCaml is known for its powerful type system, which allows programmers to express complex abstractions and ensure the correctness of their programs. It also has a number of features that make it well suited for functional programming, including higher-order functions, pattern matching, and immutable data structures. OCaml also supports object-oriented programming, and has a powerful module system that allows programmers to structure and reuse their code effectively. OCaml is actively developed and maintained, and has a large and active community of users and developers. It is used in a variety of contexts, including research, education, and commercial software development.

Unlike Lisp, I have not used OCaml, so I didn’t ask for code examples here. but I am familiar with the language theoretically enough to say that this answer is correct. OCaml is of course, the language in which the early version of Rust Compiler was implemented in, and Rust language retains certain characteristics of OCaml still to this day.

I seem to be running out of questions about languages, so I asked about Lua:

What about Lua?

Chat GPT: Yes, I am familiar with Lua. Lua is a lightweight, high-level, interpreted programming language that is widely used as an embedded scripting language in a variety of applications, including games, web servers, and image processing software. Lua is designed to be easy to learn and use, and has a simple, minimalistic syntax that makes it well suited for writing scripts and programs that need to be easy to understand and maintain. It is also a flexible language that can be extended and customized through the use of modules and libraries, which allow programmers to add new functionality to their programs without having to rewrite existing code. Lua is often used in combination with other languages, such as C and C++, to provide a scripting interface for applications. It is also used as a standalone language for creating small programs and scripts. It is an actively maintained, open-source language with a large and active community of users and developers.

As a final question, I asked about Assembly:

Do you know assembly language?

Chat GPT: Yes, I am familiar with assembly language. It is a low-level programming language that is used to directly control a computer's hardware. Assembly language instructions are directly executed by the computer's central processing unit (CPU), and it can be used to write programs for a wide variety of platforms and architectures. Assembly language is generally considered to be more difficult to use than high-level languages such as C or Python, but it can be useful for tasks that require fine-grained control over a computer's resources, such as operating system development or low-level device drivers.

Enough about languages. I wanted to know whether it can help me in solving a particular problem with which I am having some trouble with. So, I presented this next question:

Can you show me a program which can generate a list of prime numbers in a given range, written in Rust?

Chat GPT: Here is a program that generates a list of prime numbers in a given range using the Sieve of Eratosthenes algorithm, written in Rust:

fn main() {
    let mut primes = vec![true; 100]; // Create a boolean array of length 100
    primes[0] = false; // 0 and 1 are not prime
    primes[1] = false;

    for i in 2..100 {
        if primes[i] {
            for j in (i*i..100).step_by(i) {
                primes[j] = false;
            }
        }
    }

    let prime_numbers: Vec<usize> = primes
        .iter()
        .enumerate()
        .filter(|(_, &is_prime)| is_prime)
        .map(|(i, _)| i)
        .collect();

    println!("Prime numbers: {:?}", prime_numbers);
}

This program creates a boolean array of length 100, where each index represents a number, and the value at that index represents whether or not that number is prime. It then uses the Sieve of Eratosthenes algorithm to mark all non-prime numbers as false in the array. Finally, it filters out the prime numbers and collects them into a vector, which is then printed to the console. Note that this example is limited to 100. To get prime numbers in a given range, you can adjust the size of the vector and the range in the for loop accordingly.

I like its explanations, and I can’t help but notice how the code is commented. The machine is more diligent than a human, what are the days coming to? Jokes aside, I like its explanation as I have stated already, but I didn’t want this algorithm. Instead, I was looking for some brute force approach, which I can show off to someone in a pinch, for example, in my tutorial. So, I asked for a simpler version of this program.

Can you show me a simpler version of this program?

Chat GPT: Sure, here's a simpler version of the program that doesn't use a boolean array:

fn main() {
    let mut primes: Vec<u32> = vec![2];
    for i in 3..100 {
        let mut is_prime = true;
        for j in 2..i {
            if i % j == 0 {
                is_prime = false;
                break;
            }
        }
        if is_prime {
            primes.push(i);
        }
    }
    println!("Prime numbers: {:?}", primes);
}

This program starts with a vector containing the first prime number 2 and then iterates over the numbers from 3 to 100. For each number, it checks if it's divisible by any number less than it, if so, it's not a prime number, it will break the inner loop and move to next. if the inner loop completes without breaking it's a prime number, it will push to the primes vector. Note that this program is not as efficient as the previous one because it checks all numbers less than the current number to see if it's divisible by them. You can adjust the range according to your requirements.

I’m not an expert in big O notation. But can Chat GPT help me with that? so I asked the big O of its own code:

I presented it the first Rust program it generated for me, and asked for its big O. Here’s what it gave me.

Chat GPT: The big O notation describes the performance of an algorithm, specifically how the time it takes to execute grows as the input size increases. The first for loop in this program iterates from 2 to 100, which is a total of 98 iterations. This is the outer loop of the Sieve of Eratosthenes algorithm, and it has a time complexity of O(n) where n is the range of numbers we are checking for primes. The second for loop, nested inside the first one, starts at i^2 and iterates by i steps until 100. The number of iterations here depends on the value of i, which is determined by the outer loop. This inner loop is responsible for marking non-prime numbers in the boolean array as false. The upper bound of the inner loop can be found by O(sqrt(n)) where n is the range of numbers we are checking for primes. Finally, the filter and map functions used to extract the prime numbers from the boolean array have a time complexity of O(n). Overall, the time complexity of this program is O(n*sqrt(n)) and space complexity is O(n) It's important to note that this is a rough estimate of the time complexity, as the actual performance of the algorithm depends on the specific implementation and hardware it's running on.

I’ll take your word for on face value for now, machine. But what about the second program?

Chat GPT: This program is a simpler version of the first one and it finds prime numbers by iterating over a range of numbers and checking if each number is divisible by any number less than it. The outer for loop starts at 3 and goes up to 100, which is a total of 97 iterations. This is the main loop of the algorithm, and it has a time complexity of O(n) where n is the range of numbers we are checking for primes. The inner for loop starts at 2 and goes up to the current number being checked in the outer loop. This inner loop is responsible for checking if the current number is divisible by any number less than it. The upper bound of the inner loop can be found by O(sqrt(n)) where n is the range of numbers we are checking for primes. The if is_prime { primes.push(i); } will push the prime number to the vector if it's prime. Finally, the time complexity of the program is O(n*sqrt(n)) and space complexity is O(n) It's important to note that this is a rough estimate of the time complexity, as the actual performance of the algorithm depends on the specific implementation and hardware it's running on. This algorithm is not as efficient as the Sieve of Eratosthenes algorithm because it checks all numbers less than the current number to see if it's divisible by them.

Good, very good. So far, I like what I have seen. But surely this must come with a downside, right? because nothing in the field of software development is perfect. So, we now move on to…

The downsides:

Here are the downsides I can think of, or I have read on the internet from others:

  • It can reduce you to a mere editor: you have to read and modify the generated code, instead of writing everything from the scratch, so you are more like an editor / reviewer than a programmer.

  • It can produce code which is way above your head: the first Rust program it generated for me was using Sieve of Eratosthenes algorithm. Lacking the necessary knowledge about algorithms, I was not able to assess the program perfectly. This is of course, is a small example. But it can happen on a larger scale.

  • It can generate insecure code: this one I have read on the Register. Some studies were done on the AI generated code, since the developers using AI believed that they write the code which is more secure with the help of AI. But their assumption turned out to be false.

  • It can only be used with small examples: though personally I do not see it as a downside. Chat GPT cannot write full-featured programs with hundreds or thousands of lines of code, because it would take a long time to generate that code, and I personally feel it should not be given this responsibility in the first place, and I explain why in my next point…

  • Your project details can be leaked: Chat GPT warns you that you should not use anything critical to your privacy. And I think the code of your projects falls in the same category. Whatever conversations you have with it are used to improve the bot further, and as a result, details of your projects can be exposed.

Having said this, I think that the upsides are worth considering before we outright reject this technology…

The upsides:

  • It can help with problem solving: true, it won’t write the production code, but if you are having a problem with a task, you can ask for some examples, and this would speed up the development. How? For that you have to read my next point…

  • No more crawling through the web pages: on Google, you have to deal with a lot of things which are not related with your problems. You have to deal with shitty tutorial sites which are constantly trying to sell their courses to desperate students, you have to crawl through multiple pages to find one answer, and the worst thing is that when you thought you had an answer to your problem, you find out that the entire thing is unrelated with what you are searching for, and you have just wasted a lot of time. As a result, no more dealing with low-quality results, which do manage to rank on top somehow. You can directly ask Chat GPT about your problem, and the bot will provide all the help it can.

  • No need to worry about popularity: on Google and other search engines, you can only find what’s popular. If your language or framework is not popular, or it is some legacy piece of code, written in some unpopular language which is just an academic interest for computer historians, then you are out of luck. But as the example with Lisp shows, this bot is familiar with tons of things which search engines cannot provide. I am fully aware that its knowledge is not perfect, and even now, it probably won’t know some old legacy piece of code. But what it knows is more than enough for the majority of the cases, and who knows? It might improve, and acquire the missing knowledge as the time goes by.

  • No more dealing with the guilt and abuse: if you have gone to a forum, a Reddit thread, or even Stack overflow, then you have likely encountered this: your question is a duplicate, this question has already been answered (cue link to some old post from early 2000s to late 90s), or “We won’t help with your homework” people, who are always ready to say this. I think they even have some sort of short command on their keyboard for instantly copy and paste this response. But here, there is no judgement. No humiliation. No one will berate you for asking a stupid question. you can ask questions, and you will get answers. And that is all there is to it.

In the end…

It all comes down to how you use this technology. Of course, putting production code in front of this chatbot, and exposing the project details is not recommended, and you should be careful about that. but on the other hand, you can really speed up your development by asking about small examples, since you won’t have to crawl through the obscure corners of the internet, where you might end up on dark web, or get kidnapped, or end up losing a kidney or two.

Also, for now there aren’t any AI merchants who have brought the quality down, like the SEO merchants did with the search engines. And I don’t think that Chat GPT would remain free forever. But if it continues the way it has already, and improves continuously, then I wouldn’t mind paying for its services.

That is of course, if I can get some employment on the first place. Bad economy, disability, all these things are a barrier for me. you can read about the kinds of conversations I have with HR during the interview here.

As for those fictional computers? I see things like Chat GPT as extremely early prototypes of those. Maybe by the end of this century, or in the very next century, we might get those, assuming humans don’t implode.

How your experience has been with Chat GPT so far? Share in the comments!

Did you find this article valuable?

Support Tanish Shrivastava by becoming a sponsor. Any amount is appreciated!