Skip to content

Instantly share code, notes, and snippets.

@bhaumik
Created September 29, 2017 18:33
Show Gist options
  • Save bhaumik/31771b980e677fcd2520a9a8456707ca to your computer and use it in GitHub Desktop.
Save bhaumik/31771b980e677fcd2520a9a8456707ca to your computer and use it in GitHub Desktop.
# A list language
In this short assignment we're going to learn about a basic programming language which works with lists.
## Lists
In this language, a list is just a list of numbers. You can add and remove numbers from the end of the list, but you aren't allowed to insert or remove numbers from the beginning or middle of the list. Here's an example of a list containing three numbers:
```
10 24 64
```
## Modifying the list
To modify the list we can use two commands, `add` and `remove`. These add and remove numbers from the end of the list.
### The `add` command
The command for adding a number to the end of a stack is `add`. Here's an example of three `add` commands issued one after another:
```
add 2
add 3
add 5
```
If we run those commands, the contents of the list is:
```
2 3 5
```
First, two is be added to the empty list, then three is added to that, then five.
### The `remove` command
The command for removing a number from the end of the list is `remove`. Here's an example of `add` and `remove` being used together:
```
add 2
add 3
remove
add 5
add 8
remove
```
Running these commands results in a list with the following contents:
```
2 5
```
Let's run through what happened there: First, 2 and 3 were added to the end of the list. Then the 3 we just added was removed from the end of the list. We then add 5 and 8 to the end of the list, and remove the 8.
We can add some human readable information, which is ignored by the computer, to the end of each line showing the contents of the list at each point in this program. The computer ignores everything after the double forward-slash (`//`):
```
add 2 // Contents of the list is 2
add 3 // Contents of the list is 2 3
remove // Contents of the list is 2
add 5 // Contents of the list is 2 5
add 8 // Contents of the list is 2 5 8
remove // Contents of the list is 2 5
```
### Output instructions
There are two commands which can be used to output values to the screen. The `showNumber` command displays the last number in the list. Let's take a look at an example:
```
add 2 // Contents of the list is 2
showNumber // This will output the last number in the list (2)
add 3 // Contents of the list is 2 3
showNumber // This will output the last number in the list (3)
```
This would give the following output:
```
2
3
```
The `showLetter` command takes the last number in the list and outputs the corresponding letter of the alphabet. Let's look at an example:
```
add 2 // Contents of the list is 2
showLetter // This will output the letter corresponding to the last number in the list (b)
add 3 // Contents of the list is 2 3
showLetter // This will output the letter corresponding to the last number in the list (c)
```
This would give the following output:
```
b
c
```
The character `b`, the second letter of the alphabet, corresponds to the number 2 on the end of the list. And the character `c` similarly corresponds to the number 3 being on the end of the list.
## Overview
That's all you need to know about our simple programming language. Here's a quick overview of all of the commands you've learned about:
* `add n` - Adds the value `n` to the end of the list
* `remove` - Removes a value from the end of the list
* `showNumber` - Outputs the number at the end of the list
* `showLetter` - Outputs the letter corresponding to the number at the end of the list (1=a, 2=b, 3=c, etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment