Re: [stack] the concatenative wikipedia article

John Nowak <[email protected]>
Newsgroups gmane.comp.lang.concatenative
Message-ID <[email protected]>
On Dec 31, 2008, at 3:21 PM, William Tanksley, Jr wrote:

> OTOH, can a concatenative language ever be infix? I'd find it odd...

The general idea that I've had for a prefix/infix language is as  
follows:

1. 'f g' denotes the composition of 'f' and 'g'
2. 'f g h' denotes the composition of 'g' with the construction of 'f'  
and 'h' (construction is basically a cleave combinator)
3. Grouping is to the right such that 'f g h i j k == f g (h i (j k))'
4. Composition is associative such that '(f g) h == f (g h)'
5. Literals (e.g. '42') are constant functions such that '42 f == 42'
6. Multiple arguments are passed via lists
7. 'a == head', 'b == head tail', 'c == head (head tail)', etc --  
these are essentially used to index into lists

As such, you write functions like this:

    discriminant = (sq b) - 4 * a * c

And here's the definition in use

    # composition of discriminant and the construction [1,3,-4]
    discriminant [1,3,-4]
    # expansion of discriminant
    ((sq b) - 4 * a * c) [1,3,-4]
    # fully parenthesized
    ((sq b) - (4 * (a * c))) [1,3,-4]
    # replacing infix with composition + construction
    (- [sq b, * [4, * [a, c]]) [1,3,-4]
    # law of associativity: (f g) h == f (g h)
    - ([sq b, * [4, * [a, c]]) [1,3,-4])
    # law of distributivity: [f0..fN] g == [f0 g..fN g]
    - [(sq b) [1,3,-4], * [4 [1,3,-4], (* [a, c]) [1,3,-4]]
    # simplification
    - [(sq b) [1,3,-4], * [4, (* [a, c]) [1,3,-4]]
    - [(sq b) [1,3,-4], * [4, * [1, -4]]
    - [sq (b [1,3,-4]), * [4, * [1, -4]]
    - [sq 3, * [4, * [1, -4]]
    - [sq 3, * [4, -4]]
    - [sq 3, -16]
    - [9, -16]
    25

The infix syntax makes the manipulation a little awkward, but I've  
found it often makes the definitions shorter and more readable. The  
fact that any function can be used infix is a nice touch.

So, is this concatenative? It's just composition and cleave combinators.

- John
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.