Login Register


Learn the Stuck Programming Language - Thread 2: Diving Deeper filter_list
Author
Message
Learn the Stuck Programming Language - Thread 2: Diving Deeper #1
Syntax();
Learn the Stuck Programming Language - Thread 2: Diving Deeper
Click here for Thread 1
Click here for Thread 3



Table of Contents
  1. Preface
  2. Basic Built-ins
  3. Math Built-ins
  4. SciPy Built-ins
  5. The Loop Construct
  6. Putting It All Together



Chapter 1: Preface

Firstly, if you haven't read the first thread, or you need to download the Stuck interpreter, go here.

Now, if you've been through the first thread, then you've experienced the reverse Polish notation that is used to perform operations, working with inline string and number definitions, taking user input and more. Now, we get to the fun stuff - making more complex programs!

I'll be running through all of the built-ins I've made so far, which work off of a plugin system (I will cover making plugins later). Also, we will be checking out the beautiful looping construct, which will help greatly!

As a quick note, if you would ever like to see how the stack is modified after each step, at the end of your Stuck program, append a -d. This enables the debug mode, which will show you how each step of the application changes the stack.



Chapter 2: Basic Built-ins

Here's a list of all of the basic built-in functions that will help with organizing and modifying the stack.
  • y removes the top item from the stack.
  • , removes all but the top item from the stack.
  • ] will flatten a list and place it's components on the stack.
  • [ will wrap all the elements on the stack in a list.
  • c will map a string or list of characters to their ASCII values.
  • d will map a list of ASCII values to a string.
  • _ duplicates the top item on the stack.
  • ~ evaluates the top item on the stack. (This is very powerful!)
  • r takes the number on the top of the stack and creates a list of numbers from 0 to n-1.
  • R takes the number on the top of the stack and creates a list of numbers from 0 to n-1.
  • p prints the top item on the stack. This also suppresses automatic printing at the end of execution.
  • z will, by default, zip together the top two lists. If a number is found on the top of the stack, it will zip the top n lists together.
  • Z will, by default, zip by longest the top two lists. If a number is found on the top of the stack, it will zip by longest the n lists together.
  • m reads a string of Python lambda syntax from the top of the stack, and then maps the top list of the stack based on that lambda.
  • f reads a string of Python lambda syntax from the top of the stack, and then filters the top list of the stack based on that lambda.
  • l will return the length of the top item of the stack, if it is a list or string. If not, it returns the length of the stack.
  • N is a reserved character, which will add a newline character to the stack.
  • j takes a separator off the top of the stack, and then takes the top list and joins it by the separator.
  • Q takes a string, substring and replacement from the stack, and replaces all instances of substring with replacement in the string.

A few notes concerning some commands listed above, which should clear up functionality:
  • z and Z function differently, which is why I specified zip by longest for Z. By default, Python's zip function will zip lists together, and stop once the shortest list is exhausted of elements. However, in some instances you may want to just have null values so you can zip up to the longest list, which is why Z exists.
  • ~ is the equivalent of the eval function in Python. This means, that while the language does not support inline lists, you can wrap a list in a string, and use ~ to get the list out of it. It is not just limited to lists though, anything Python can eval, ~ will work with. To be clear:
    Code:
    "This is an example""[1,2,3]"~ results in a stack of ["This is an example", [1, 2, 3]]



Chapter 3: Math Built-ins

A great use of Stack is to perform calculations, so of course there has to be a math library. To see more of the basic operations, refer to the first thread.
  • ` takes the top list from the stack, and checks how long it is. If it's of length 4 or 2, it will evaluate the length. If the top item isn't a list, the top 4 numbers are taken and then length is calculated.
  • Σ performs summation on the top list, or the stack if the top item is not a list.
  • Π calculates the product of the top list, or of the stack if the top item is not a list.
  • ! calculates the factorial of the top number on the stack.
  • | calculates the absolute value of the top number on the stack.
  • ( rounds the top value of the stack up (ceiling).
  • ) rounds the top value of the stack down (floor).
  • X gets the top two lists from the stack, and calculates the Cartesian product. If the top element is a number, it calculates the Cartesian product of the top n lists.
  • Ï€ is a reserved character, which puts the value of pi on the stack.
  • e is a reserved character, which puts the value of e on the stack.

I will be implementing logarithms, permutations and combinations at some point, I just haven't gotten around to it. For those of you unaware of the Cartesian product, this is it's basic function:

Code:
Let's say the stack is [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]], and we use X for Cartesian product The output would be the 120 different combinations: [(1, 1, 1, 1, 1), (1, 1, 1, 1, 2), (1, 1, 1, 1, 3), (1, 1, 1, 1, 4), (1, 1, 1, 1, 5), (1, 1, 1, 2, 1), (1, 1, 1, 2, 2), (1, 1, 1, 2, 3), (1, 1, 1, 2, 4), (1, 1, 1, 2, 5), (1, 1, 1, 3, 1), (1, 1, 1, 3, 2), (1, 1, 1, 3, 3), (1, 1, 1, 3, 4), (1, 1, 1, 3, 5), (1, 1, 1, 4, 1), (1, 1, 1, 4, 2), (1, 1, 1, 4, 3), (1, 1, 1, 4, 4), (1, 1, 1, 4, 5), (1, 1, 2, 1, 1), (1, 1, 2, 1, 2), (1, 1, 2, 1, 3), (1, 1, 2, 1, 4), (1, 1, 2, 1, 5), (1, 1, 2, 2, 1), (1, 1, 2, 2, 2), (1, 1, 2, 2, 3), (1, 1, 2, 2, 4), (1, 1, 2, 2, 5), (1, 1, 2, 3, 1), (1, 1, 2, 3, 2), (1, 1, 2, 3, 3), (1, 1, 2, 3, 4), (1, 1, 2, 3, 5), (1, 1, 2, 4, 1), (1, 1, 2, 4, 2), (1, 1, 2, 4, 3), (1, 1, 2, 4, 4), (1, 1, 2, 4, 5), (1, 1, 3, 1, 1), (1, 1, 3, 1, 2), (1, 1, 3, 1, 3), (1, 1, 3, 1, 4), (1, 1, 3, 1, 5), (1, 1, 3, 2, 1), (1, 1, 3, 2, 2), (1, 1, 3, 2, 3), (1, 1, 3, 2, 4), (1, 1, 3, 2, 5), (1, 1, 3, 3, 1), (1, 1, 3, 3, 2), (1, 1, 3, 3, 3), (1, 1, 3, 3, 4), (1, 1, 3, 3, 5), (1, 1, 3, 4, 1), (1, 1, 3, 4, 2), (1, 1, 3, 4, 3), (1, 1, 3, 4, 4), (1, 1, 3, 4, 5), (1, 2, 1, 1, 1), (1, 2, 1, 1, 2), (1, 2, 1, 1, 3), (1, 2, 1, 1, 4), (1, 2, 1, 1, 5), (1, 2, 1, 2, 1), (1, 2, 1, 2, 2), (1, 2, 1, 2, 3), (1, 2, 1, 2, 4), (1, 2, 1, 2, 5), (1, 2, 1, 3, 1), (1, 2, 1, 3, 2), (1, 2, 1, 3, 3), (1, 2, 1, 3, 4), (1, 2, 1, 3, 5), (1, 2, 1, 4, 1), (1, 2, 1, 4, 2), (1, 2, 1, 4, 3), (1, 2, 1, 4, 4), (1, 2, 1, 4, 5), (1, 2, 2, 1, 1), (1, 2, 2, 1, 2), (1, 2, 2, 1, 3), (1, 2, 2, 1, 4), (1, 2, 2, 1, 5), (1, 2, 2, 2, 1), (1, 2, 2, 2, 2), (1, 2, 2, 2, 3), (1, 2, 2, 2, 4), (1, 2, 2, 2, 5), (1, 2, 2, 3, 1), (1, 2, 2, 3, 2), (1, 2, 2, 3, 3), (1, 2, 2, 3, 4), (1, 2, 2, 3, 5), (1, 2, 2, 4, 1), (1, 2, 2, 4, 2), (1, 2, 2, 4, 3), (1, 2, 2, 4, 4), (1, 2, 2, 4, 5), (1, 2, 3, 1, 1), (1, 2, 3, 1, 2), (1, 2, 3, 1, 3), (1, 2, 3, 1, 4), (1, 2, 3, 1, 5), (1, 2, 3, 2, 1), (1, 2, 3, 2, 2), (1, 2, 3, 2, 3), (1, 2, 3, 2, 4), (1, 2, 3, 2, 5), (1, 2, 3, 3, 1), (1, 2, 3, 3, 2), (1, 2, 3, 3, 3), (1, 2, 3, 3, 4), (1, 2, 3, 3, 5), (1, 2, 3, 4, 1), (1, 2, 3, 4, 2), (1, 2, 3, 4, 3), (1, 2, 3, 4, 4), (1, 2, 3, 4, 5)]



Chapter 4: SciPy Built-ins

I'll be the first to admit, this module is.. sparse. I haven't gotten around to implementing a lot of SciPy's functionality, because I need to think of how exactly I want everything to work. I have so far only implemented two functions:
  • İ calculates the inverse of the matrix on the top of the stack.
  • ÆŠ calculates the determinant of the matrix on the top of the stack.

Since there's only two functions so far, I'll show implementations. These are full programs that you can run yourself to check, by the way! Make sure you have SciPy and NumPy installed.

Code:
"[[1,4,0],[0,1,3],[2,0,1]]"~İ -> [[0.04, -0.16, 0.48], [0.24, 0.04, -0.12], [-0.08, 0.32, 0.04]] "[[1,4,0],[0,1,3],[2,0,1]]"~Ɗ -> 25 "[[1,4,0],[0,1,3],[2,0,1]]"~İƊ -> 0.04



Chapter 5: The Loop Construct

Now, we reach my favourite part, learning about the almighty loop construct! This little guy, :, is probably the most useful feature implemented to date, and it was only added a day or two ago! A side effect of this is that it's not heavily tested and probably buggy, but in the tests I've done it works great!

The looping construct works by taking the top value from the stack. This value should be a string literal, which contains a valid Stuck program. Then, if the second-top value is a list, it will modify that. If it is not a list, every value on the stack will be modified. For every value of the list that the : is accessing, it initializes the stack with each value, and executes the code individually, finally returning the each of the results in the appropriate format.

As of right now, this can be nested once. What I mean by this, is you can have up to one nesting of a loop inside a loop. For example:

Code:
5R"R": will produce a stack with [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]] as the top value. 5R"R''R'':": will produce a stack with [[[1]], [[1], [1, 2]], [[1], [1, 2], [1, 2, 3]], [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]], [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]] as the top value.

Note that if you are nesting a loop, you must use double apostrophes ('') rather than quotes (") on the inside.



Chapter 6: Putting it All Together

The program I will be creating as an example is one that generates the tetrahedral number sequence. As you can see from the this, the easy way to calculate the nth tetrahedral number is n(n+1)(n+2)/6. Since we need to use the value for n three times, we'll take n via user-input, then duplicate it twice.

Code:
i__

For one term, we need to add one, and for another, we need to add two. So, we can add 1 to the top element, swap the top 2, then add 2 to the new top element. We know we need to multiply these together, and instead of using * twice, we'll use the product character. Then, we can divide by 6.

Code:
i__1+;2+Π6/

There's the basic code for calculating the Nth tetrahedral number. However, we want to generate the sequence, so we'll take the input as the max number to generate. We can convert this to a range of 1 to n, then for each k in there, we'll calculate the kth tetrahedral number. So, we will wrap this code we already have in quotes (removing the user input at the start), then use the looping construct to apply this.

Code:
iR"__1+;2+Π6/":

However, we can make this prettier. As of now, this will wait to calculate every individual value before printing the stack. If we want to see the progression of calculations, we can put in a p at the end of the loop string to print each value out as it is calulated. This will also suppress printing the stack at the end.

Code:
iR"__1+;2+Π6/p":

For a finishing touch, why don't we try to make it a little user friendly? If you want to have an input prompt, you can push a string to the stop of the stack, print it, then pop it off the stack so it doesn't interfere. I'll put the finished program below, and show how it would execute.

Code:
"Calculate the tetrahedral numbers up to:"pyiR"__1+;2+Π6/p": After execution: Calculate the tetrahedral numbers up to: (input here, for example 6) 1.0 4.0 10.0 20.0 35.0 56.0

There you go! You've made a very simple, but useful program in Stuck!



Stay tuned for the next thread, where I go into creating plugins for Stuck.
[Image: CDUAq9d.png]

Reply







Users browsing this thread: