A catalog of example Pounce programs.
Pounce is a Concatenative language, influenced by Forth, Postscript, Joy, Cat and Kitten.
A Concatenative language provides for the "composition of functions" as a first class operation and programs are constructed by composing functions together. For more information on Concatenative languages, concatenative.org is a comprehensive resource. Additionally the concatenative programming paradigm
Examples are interactive (code in blue boxes
) so you can edit the code (try entering 21 2 *
below) and see results in the orange box
below.
2
Enjoy exploring Pounce!
Pounce is a post-fix language. Notice that data (nouns) come first followed by verbs. For instance, when adding three and five, the nouns are 3 5
followed by the verb +
. Then multiply by seven 7 *
. Post-fix eliminates any need for operator-precedence rules or parentheses.
56
Words can be placed in a list where they are considered as nouns. For example cons
is used to make a list of 10 and *
[10 *]
A list is sometimes called a 'phrase' when the intent is to recite the phrase as a verb at some later time, like this:
50
To make this sequence, first the stack is seeded with 0 1
, then adding more elements is done with dup2
which duplicates the top two elements of the stack. Adding them together creates the next Fibonacci number. Repeat this n times.
0 1 1 2 3 5 8
To define a new word, two lists are needed: first a composition of words (a phrase) and then the name of the new word wrapped in a list. In post-fix style, compose
comes after and adds this new word.
42
A 'factorial' function can be made using recursion. This composition of words is added to the dictionary and given the name fac
and fac
includes in its own phrase.
As with any recursive function, an 'if' clause is needed to test for the terminal case.
120
linrec
for Linear RecursionA second version of 'factorial', this time without having to compose and name your own (recursive) word. Instead, linear recursion (linrec
) does this with four short phrases.
120
crouch
and pounce
allow references to stack values by nameThe order of stack values is important, so crouch
and pounce
let you refer to them byh name. These are not variables, more like 'named' stack items.
For example: if 5 7
are on the stack. A list of names [a b]
a is matched with 5 and b with 7
When a name is used in the phrase, it will be replaced with the value from the relative stack position. This is more convenient than doing lots of stack shuffling to achieve the same result.
Here is the formula for the slope of a line. OK, this may not be 'point-free', but no variables are bound in the process, so think of this as coping 'in' of stack values into your phrase.
7
A phrase to calculate the prime factors uses linrec
to recursively break a number down to its prime factors.
[53 2 2]
Stating with a list of numbers, map
runs a phrase (as a function) on each element of the list [2 *]
.
[12 6 16 8 10 14 4 18 2]
filter
a list with a match phrase (even numbers) [2 % 0 ==]
.
[6 8 4 2]
reduce
a list by starting with 1
and accumulating a value with [*]
362880
Combining reduce
with pounce
can make a more complex reducer easier to understand
[497 7]
Given a list of numbers to sort, binary recursion. binrec
is used to process both sub lists made by [>] split
[1 2 3 4 5 6 7 8 9]
A list of strings, same deal.
[a b c d e f g h i]