Natural Language Syntax in Ellex
Natural Language Syntax in Ellex
Section titled “Natural Language Syntax in Ellex”Ellex is designed to make programming accessible to young learners by using a syntax that feels like speaking or writing in everyday language. Instead of memorizing complex symbols and structures, you can write commands that read like simple instructions. This guide will walk you through the core elements of Ellex’s natural language syntax, helping you start coding with ease.
Why Natural Language?
Section titled “Why Natural Language?”Traditional programming languages often require learning specific syntax rules, such as semicolons, brackets, or parentheses, which can be intimidating for beginners. Ellex removes these barriers by allowing you to write code that mirrors how you might give instructions to a friend. For example, instead of writing print("Hello!")
, in Ellex, you simply write:
tell "Hello!"
This approach lets you focus on the logic of what you want to achieve rather than worrying about syntax errors. As you grow more comfortable with coding, Ellex also provides a pathway to learning full Elixir syntax, a powerful language used in real-world applications.
Basic Commands
Section titled “Basic Commands”Let’s explore the fundamental commands in Ellex that form the basis of its natural language syntax. Each command is designed to be intuitive and easy to remember.
Outputting Text with tell
Section titled “Outputting Text with tell”The tell
command is used to display text on the screen. It’s like telling someone a message. Here’s how it works:
tell "Welcome to Ellex!"
When you run this, Ellex will output “Welcome to Ellex!” to the screen. You can use this command to show messages, results, or anything you want the user to see.
Getting Input with ask
Section titled “Getting Input with ask”The ask
command lets you request input from the user and store it in a variable for later use. Think of it as asking a question:
ask "What's your name?" → nametell "Hello, {name}!"
In this example, Ellex prompts the user with “What’s your name?”. Whatever the user types is saved in the variable name
. Then, the tell
command uses {name}
to include the user’s input in the greeting, like “Hello, Alice!”.
Combining Commands
Section titled “Combining Commands”You can combine multiple commands to create small programs. Here’s an example that asks for a favorite color and responds:
ask "What's your favorite color?" → colortell "That's awesome! I like {color} too!"
Control Flow with Natural Language
Section titled “Control Flow with Natural Language”Ellex allows you to control the flow of your program using commands that sound like instructions you’d give in real life. This includes repeating actions and making decisions based on conditions.
Repeating Actions with repeat
Section titled “Repeating Actions with repeat”The repeat
command lets you loop over a set of instructions multiple times. It’s like saying, “Do this a few times.” Here’s an example:
repeat 3 times: tell "This message will show three times!"
This code will output the message three times in a row. Notice the colon :
at the end of the repeat
line, which tells Ellex that the following indented lines are part of the loop. Indentation (spaces or tabs at the start of a line) is important in Ellex to show which commands belong together.
Making Decisions with when
Section titled “Making Decisions with when”The when
command helps your program make decisions based on conditions. It works like saying, “If this is true, then do that.” Here’s how to use it:
ask "How old are you?" → agewhen {age} > 10: tell "You're over 10 years old!"else: tell "You're 10 or younger!"
In this example, Ellex checks if the user’s input for age
is greater than 10. If it is, the first message is shown; otherwise (else
), the second message appears. You can use operators like >
(greater than), <
(less than), =
(equal to), and others to compare values.
Creating Reusable Code with make
Section titled “Creating Reusable Code with make”Sometimes, you want to reuse a set of commands multiple times in your program. The make
command lets you define a function—a named block of code you can call whenever needed. Think of it as making a recipe you can follow later:
make greet_user: ask "What's your name?" → name tell "Nice to meet you, {name}!"call greet_user
Here, make greet_user
defines a function called greet_user
that asks for a name and gives a greeting. The call greet_user
line runs that function. You can call it as many times as you like, and each time it will ask for input and respond.
Working with Variables
Section titled “Working with Variables”Variables in Ellex are like containers where you store information, such as a user’s input or a calculated value. You’ve already seen variables in action with ask ... → name
. You can also set variables directly using set
:
set favorite_number to 7tell "My favorite number is {favorite_number}!"
This sets favorite_number
to 7 and then displays it in a message. Variables let you keep track of data and use it throughout your program.
Natural Language Tips
Section titled “Natural Language Tips”Ellex is forgiving with how you phrase some commands, but consistency helps when learning. Here are a few tips to make your code work smoothly:
- Use Quotes for Text: Always put text messages in quotes, like
"Hello!"
. This tells Ellex it’s a string of text, not a command. - Indent Blocks: When using
repeat
,when
, ormake
, indent the lines inside those blocks to show they belong together. - Variables with
{}
: When including a variable in text, wrap it in curly braces, like{name}
, so Ellex knows to replace it with the stored value.
Examples to Try
Section titled “Examples to Try”Here are a few small programs to practice natural language syntax. Type these into the Ellex REPL (start it with make dev
) and see how they work!
Counting Game
Section titled “Counting Game”set count to 1repeat 5 times: tell "Count: {count}" set count to {count} + 1
This counts from 1 to 5, showing each number on a new line.
Guess the Number
Section titled “Guess the Number”set secret to 3ask "Guess a number between 1 and 5!" → guesswhen {guess} = {secret}: tell "Correct! You guessed it!"else: tell "Sorry, the number was {secret}. Try again!"
This game sets a secret number (3) and asks the user to guess it, giving feedback based on the input.
Moving Beyond Basics
Section titled “Moving Beyond Basics”Once you’re comfortable with these commands, you can explore more advanced features of Ellex’s syntax:
- Nested Blocks: Put loops or conditionals inside other loops or conditionals for complex logic.
- Lists and Collections: Work with multiple items, like a list of names or numbers.
- Turtle Graphics: Use drawing commands like
move forward 50
for visual output (see the Turtle Graphics guide).
Ellex’s natural language syntax is just the beginning. As you grow, the language can introduce you to Elixir, a professional programming language, building on the skills you’ve learned here.
Getting Help with Syntax Errors
Section titled “Getting Help with Syntax Errors”If your code doesn’t work as expected, Ellex provides kid-friendly error messages with emojis and suggestions to help fix mistakes. For example, if you forget quotes around text, Ellex might say, “Did you mean to put quotes around your message? Try tell 'Hello!'
😊”. This feedback helps you learn without frustration.
Why It Works for Learning
Section titled “Why It Works for Learning”Using natural language makes coding in Ellex feel like storytelling or giving instructions, which is a great way for young coders to start. It reduces the cognitive load of learning syntax rules and lets you experiment with ideas right away. Over time, you’ll see how these natural commands map to concepts in other programming languages, preparing you for further learning.
Ready to practice? Open the Ellex REPL with make dev
and try writing your own program using tell
, ask
, repeat
, and when
. If you get stuck, check out the examples in this documentation or ask for help in our community forums (coming soon)!