# ChatGPT

Just wanted to mention, that chatGPT is aware of Crystal and can write and/or explain Crystal code (at least to some degree). It’s not in a way that one can just lean back and let the AI do all the work, and it’s only to some amount reliable. But at least it’s in general “supported”. I was somewhat surprised and find it to be not totally useless. Distrust in the answers is absolutely justified and a must (as the AI will just make things up if “necessary”), but some of the answers (to some very simple questions) were very useable and would compile (and do what was requested); but there were also moments in which the code wouldn’t compile. It doesn’t replace other resources (and sadly isn’t reusable by others as well - unlike for example posts in the Help & Support forum) but if you need a quick answer, it’s probably worth giving it a try.

Here is an example:

Me:

write a method to calculate fibonacci numbers in Crystal

ChatGPT:

Certainly! Here is a method for calculating Fibonacci numbers in Crystal:

``````def fibonacci(n : Int64) : Int64
if n <= 0
return 0
elsif n == 1
return 1
else
return fibonacci(n-1) + fibonacci(n-2)
end
``````

This method uses recursion to calculate the Fibonacci number at the given index `n`. The base cases are when `n` is less than or equal to 0, in which case the function returns 0, and when `n` is 1, in which case the function returns 1. For all other values of `n`, the function calculates the Fibonacci number by adding the previous two Fibonacci numbers (which are calculated recursively).

Here’s an example of how you can use this method:

``````puts fibonacci(0) # prints 0
puts fibonacci(1) # prints 1
puts fibonacci(2) # prints 1
puts fibonacci(3) # prints 2
puts fibonacci(4) # prints 3
puts fibonacci(5) # prints 5
``````

I hope this helps! Let me know if you have any questions.

If you haven’t used it so far, this is how you get the answers presented (if you use directly their website and not any client via api).

Although the code is neither correct nor good nor efficient in this case, but it might still be enough help if one was stuck with some problem.

Also: the AI gets really tricky to solve some problems and will use shards which don’t exist and it just made up (which can be useful occasionally as well, as the naming might be close to existing c libraries)

And here are two more examples of possible questions/ answers (sorry to everyone who uses the forum with some translation - this time it’s just the picture as it’s too much text).

Not amazing and always to be taken with some doubt and distrust, but imho it can be useful sometimes, and it’s easy and quick to access.

Maybe it helps someone

4 Likes

Indeed!

I recently sent a PR to Crystalline to add missing "end"s to a program before parsing it (for the program’s structure, autocompletion, etc.) and I sat a few minutes thinking about an algorithm to do that. I concluded that I could use a stack to keep track of lines that “open a block”, and keep track of the indentation. Then if I find a line that’s not an “end” but it has the same (or less) indentation of the opening block then it must be a mistake and an “end” is missing there.

After doing the entire PR I was curious and gave the problem to ChatGPT.

As you mention, the program the AI gave me is neither correct (it doesn’t account for indentation) nor it compiles (it lacks some type annotations) but without an initial idea it would have at least given me the idea of using a stack, or the general algorithm’s idea:

It’s funny because the code has a bit of Ruby (`[]` is not valid in Crystal) and a bit of Python (the `"""` thing for strings)

4 Likes