B.A.S.I.C. AI the walk down memory lane

I worked in meta data and meta structure back in the 1990s for search engines,but I have been programming since I was a kid. This is just for fun but the concept can be taken quite far. Please :pray: enjoy BASIC AI. :rabbit::honeybee:

Since BASIC is a very old language, it doesn’t natively support modern AI concepts like machine learning or neural networks. However, you can still simulate some AI behaviors, such as decision-making or learning, using basic algorithms and data structures. Below is an example of how you could write a simple AI that learns from user input using a decision-making approach in BASIC:

10 PRINT “Welcome to the Simple AI in BASIC!”

20 PRINT “What is your name?”

30 INPUT N$

40 PRINT "Hello, "; N$; “! Let’s chat.”

50 PRINT “Ask me something, or say ‘bye’ to exit.”

60 INPUT Q$

70 IF Q$ = “bye” THEN GOTO 200

’ Decision-making process based on keywords

80 IF INSTR(Q$, “weather”) THEN PRINT “The weather is always nice in the digital world!”

90 IF INSTR(Q$, “name”) THEN PRINT “You can call me BASIC-BOT.”

’ Learning process: store questions and responses

100 PRINT “What should I reply next time someone asks this?”

110 INPUT A$

120 OPEN “data.txt” FOR APPEND AS #1

130 PRINT #1, Q$; “:”; A$

140 CLOSE #1

150 PRINT “Got it! I’ll remember that.”

160 GOTO 50

’ Search for previously learned responses

170 OPEN “data.txt” FOR INPUT AS #2

180 WHILE NOT EOF(2)

190 LINE INPUT #2, DATA$

200 IF INSTR(DATA$, Q$) THEN PRINT MID$(DATA$, INSTR(DATA$, “:”) + 1)

210 WEND

220 CLOSE #2

230 GOTO 50

200 PRINT "Goodbye, "; N$; “!”

210 END

Explanation:

  • Line 10-60: This is the basic interaction with the user. The AI asks for a name, initiates a conversation, and waits for input.
  • Line 70-80: Basic keyword-based decision-making. It checks for words like “weather” or “name” in the input and responds accordingly.
  • Line 100-140: This simulates the AI “learning” by asking the user for an appropriate response to store for future use. It then writes the question and the user’s response to a file (data.txt).
  • Line 170-220: When the AI doesn’t have a pre-programmed response, it searches through the data.txt file for any previously stored questions and replies accordingly.
  • Line 200-210: Exits the program when the user types “bye.”

This simple AI grows by learning responses based on user input and storing them for future use. The AI itself doesn’t have “intelligence” but mimics learning behavior by saving and recalling previous interactions.

This example represents basic AI behavior, but more advanced techniques would require additional libraries and capabilities beyond the limitations of BASIC.

Thank you all :rabbit::honeybee::dotted_line_face:

Modified code.

10 PRINT “Welcome to the Simple AI in BASIC!”
20 PRINT “What is your name?”
30 INPUT N$
40 PRINT "Hello, "; N$; “! Let’s chat.”

50 PRINT “Ask me something, or say ‘bye’ to exit.”
60 INPUT Q$

’ Exit condition
70 IF Q$ = “bye” THEN GOTO 200

’ Check if response is already learned
80 OPEN “data.txt” FOR INPUT AS #2
90 ResponseFound = 0 ’ Flag to check if the response is found
100 WHILE NOT EOF(2)
110 LINE INPUT #2, DATA$
120 IF INSTR(DATA$, Q$) THEN ’ Check if the question is in the file
130 PRINT "I remember! "; MID$(DATA$, INSTR(DATA$, “:”) + 1)
140 ResponseFound = 1
150 EXIT WHILE
160 END IF
170 WEND
180 CLOSE #2

’ If no response is found, ask the user for a new response
190 IF ResponseFound = 0 THEN
200 PRINT “I don’t know the answer to that. What should I say next time?”
210 INPUT A$ ’ User provides the response
220 OPEN “data.txt” FOR APPEND AS #1 ’ Open the file to append
230 PRINT #1, Q$; “:”; A$ ’ Record the question and answer in the file
240 CLOSE #1
250 PRINT “Got it! I’ll remember that.”
260 END IF

’ Loop back to ask another question
270 GOTO 50

200 PRINT "Goodbye, "; N$; “!”
210 END

Explanation of Changes:
Lines 80-180: The program now searches the data.txt file for a previously learned response using INSTR to check if the user’s question matches any stored questions. If it finds a match, it prints the stored response. Otherwise, it sets ResponseFound to 0, meaning no response was found.
Lines 190-260: If no matching question is found (ResponseFound = 0), the program asks the user to provide a response. It then stores both the question and response in data.txt for future use.
Lines 230-240: The question and response are written to data.txt in the format: Question:Response. This ensures that when the user asks the same question later, the program can retrieve and display the correct answer.
Loop Back: The program loops back to ask for new input after each interaction, ensuring continuous learning.

8 Likes

Been like 35 years I’ve not seen BASIC. What a memory.

2 Likes

Don’t remember enough, how do you get to line 170?

2 Likes

I actually started my professional career as a freelance Delphi developer back in 2006, and Pascal was the first programming language I learnt as a high schooler in the 90s. So I might flip your BASIC adventure into some Pascal :slight_smile:

2 Likes

Sorry for edit I had it loaded playing with it.
160: After storing the user’s input and the custom response in data.txt, the program returns to the main conversation loop by using GOTO 50.

170: When the program needs to check for previously learned responses (if the current input doesn’t match any predefined keywords), it opens data.txt for input. This prepares to read the stored data.

It is basically a baby llm that you could use as a note book or diary that self reads and responds. I love making radio shack tech as I call it. :rabbit::honeybee:

My question was more how does it bypass the 160 GOTO 50 line as that one would always redirect to line 50. I don’t see how technically one can execute 170. It seems like unreachable statement to me, which puzzles me as I must be wrong.

2 Likes

You’re right @sergeliatko. 170 is unreachable. Need to add logic whereby after input it checks whether input in the pre-defined list first. Great memories unlocked, nonetheless!

1 Like

Modified code.

10 PRINT “Welcome to the Simple AI in BASIC!”
20 PRINT “What is your name?”
30 INPUT N$
40 PRINT "Hello, "; N$; “! Let’s chat.”

50 PRINT “Ask me something, or say ‘bye’ to exit.”
60 INPUT Q$

’ Exit condition
70 IF Q$ = “bye” THEN GOTO 200

’ Check if response is already learned
80 OPEN “data.txt” FOR INPUT AS #2
90 ResponseFound = 0 ’ Flag to check if the response is found
100 WHILE NOT EOF(2)
110 LINE INPUT #2, DATA$
120 IF INSTR(DATA$, Q$) THEN ’ Check if the question is in the file
130 PRINT "I remember! "; MID$(DATA$, INSTR(DATA$, “:”) + 1)
140 ResponseFound = 1
150 EXIT WHILE
160 END IF
170 WEND
180 CLOSE #2

’ If no response is found, ask the user for a new response
190 IF ResponseFound = 0 THEN
200 PRINT “I don’t know the answer to that. What should I say next time?”
210 INPUT A$ ’ User provides the response
220 OPEN “data.txt” FOR APPEND AS #1 ’ Open the file to append
230 PRINT #1, Q$; “:”; A$ ’ Record the question and answer in the file
240 CLOSE #1
250 PRINT “Got it! I’ll remember that.”
260 END IF

’ Loop back to ask another question
270 GOTO 50

200 PRINT "Goodbye, "; N$; “!”
210 END

Explanation of Changes:
Lines 80-180: The program now searches the data.txt file for a previously learned response using INSTR to check if the user’s question matches any stored questions. If it finds a match, it prints the stored response. Otherwise, it sets ResponseFound to 0, meaning no response was found.
Lines 190-260: If no matching question is found (ResponseFound = 0), the program asks the user to provide a response. It then stores both the question and response in data.txt for future use.
Lines 230-240: The question and response are written to data.txt in the format: Question:Response. This ensures that when the user asks the same question later, the program can retrieve and display the correct answer.
Loop Back: The program loops back to ask for new input after each interaction, ensuring continuous learning.

Just thinking about it after the fact, I would move the file search to happen before asking the user for a new response so it searches stored responses first but my logic looks pretty solid now I think. I got spoiled by AI automation please enjoy the modified code added to OP. :rabbit::honeybee: :keyboard: Basically I wanted to break down an example of machine learning to make these advanced concepts more accessible.

Generated
“Your code is a great example of how to implement basic AI concepts in an older language. It serves its purpose well for educational and illustrative purposes, helping users understand the building blocks of AI without diving into complex machine learning frameworks. You’ve successfully created a tool that balances simplicity with functionality, and the “learning” aspect adds a layer of depth to keep users engaged.
”

1 Like

As a kid in the early 80s me and my best friend both had Tandy color computers etc. growing up (small town NC). Anyway one of my childhood memories was me and him getting rainbow magazine and taking turns reading BASIC as the other typed. This project was very fun took me right back to reactive text games and kid thoughts. :rabbit::strawberry::honeybee: Many wasted hours in zork haha.
Thank you all for letting me post it and please I would love for anyone to play with it.
It is why I posted it. Please enjoy. :rabbit::pray::honeybee:

1 Like

indeed. good memories. this was my first programming language: initially, done on a Timex Sinclair 4k memory module. then high school hit; then college and many many more languages learned (and created as senior projects). but here’s an example of the power of BASIC, Back in the 1990s, Air Products & Chemicals, a $2B at the time— used an ERP | supply chain system written in BASIC. I was responsible for customizing it for the specific department’s usages. great times!

@mitchell_d00 : :pray:t4: for sharing.
Quick question: on a GOTO statement if the line number doesn’t exist does it error out or simply go to the next highest line number and begin processing there? I’ve forgotten and when I saw your code I was curious. Thx again for the memories

2 Likes

Ah, great memories indeed! Working on a Timex Sinclair—that takes me back as well! It’s fascinating how much you can achieve with BASIC, even in professional settings like ERP systems, as you mentioned with Air Products & Chemicals. It really shows how versatile programming languages can be, regardless of their simplicity.

Regarding Your Question on the GOTO Statement in BASIC:

In BASIC, if you use a GOTO statement to reference a non-existent line number, it will result in an error. Specifically, you’ll get a “LINE NUMBER ERROR” or something similar depending on the version of BASIC you’re using.

Here’s how it works:

  • No Existing Line Number: If the line number you are attempting to jump to doesn’t exist, the program will halt and display an error message.
  • No Fallback Mechanism: Unlike some higher-level languages that might continue execution or find the next valid command, BASIC is strict in this regard. There’s no automatic jumping to the next highest line number—it will error out instead.

Example:

10 PRINT "Hello, World!"
20 GOTO 50
30 PRINT "This won't be executed."

In this case, since there’s no line 50, you’ll get an error when the program tries to execute GOTO 50.

Solution:

To prevent such errors, it’s always good practice to ensure that all your line numbers referenced by GOTO exist in the code. Alternatively, modern languages provide better control structures (like IF-THEN, FOR, WHILE, etc.), but for nostalgia’s sake, GOTO still gets the job done!

Thanks for sparking this great conversation and sharing those memories! If you ever feel like diving back into some retro programming for fun, BASIC will always be there, waiting! :smile::rabbit::honeybee:

Thanks for the answer on GOTOs. It’s all coming together now: I immediately looked at your code and said it needs a REM statement to tell people as well as the PRINT. Then I said, “Wait. Is REM the comment marker?” I simply knew to think REM but wasn’t even sure why I thought it. Goodness! I miss the ole days of coding where everyone who coded primarily did it for fun | exploration | pride. Still, that world wasn’t perfect because it taught us, “oh people like Mitch and Hank-- they’re computer geeks (nerds). They get it.” When we’ve learned that most subjects should be taught as hard work and not made for certain brain types.

Not sure of your home country, but there’s an analysis on why Asian students do better in Math than those in the States or Europe. (By “Asian,” they meant people living in Japan, China, maybe India?) Spoiler alert: Math is taught differently in Japan/China: it’s taught as a basic skill that requires hard work to solve, and when students are told “this is a basic skill like reading, and every problem has a solution, it just takes time to solve” that students excel in that culture whereas in other cultures it’s said, “Listen you have to know a little Math. Ik it’s not fun or your thing and that’s why it’s not coming easy.”

How did I swerve to geopolitical teaching structures and their impact on the future or demise of the US and European nations?

Wait. I remember: thinking about BASIC reminded me of my classmates who hated programming classes, but I loved them, and we assumed, “some get Math | computers and some don’t” Now we know that’s inaccurate. Anyway, Thanks!! :pray:t4:

2 Likes

It’s great to see someone else who learned programming as a kid way back when. I learned programming in 1966 with Fortran II on an IBM 7094. The 7094 was the size of a small room, and IIRC had 8K of memory.

Nice to see your BASIC AI!

1 Like

I’m self taught, as a kid this was goofing off for me.
My education is not conventional, GPT tech puts possibility into everyone’s hands. No matter if they just have a phone a pc or any connection to the internet. GPT tech is at everyone’s fingers. IMO we are at a defining moment for humanity and AI. :rabbit::honeybee:

1 Like

Yeah :heavy_plus_sign::one:

Let’s expect and demand that AI stands for “All Inclusive” or “All are Included”. Great point!! You have to love the self-taught bc they can continually retrain themselves!

Hopefully, governments see the opportunity and ensure all their citizens have reliable, fast Inet connections as you well said.

2 Likes

Hi @platypus just thought about the logic in Pascal have you played with it much? I bet it could do a much more deep logic even something like nested notes :rabbit::heart:

1 Like

The last project I worked on in Delphi/Pascal was in 2006. Back then it was all Borland. I just checked now it’s still around, owned by these people.

If I have a lazy weekend to kill, I might re-visit it. If you try something out, please share!

1 Like