Front Page All Articles Recent Changes Random Article

Contents

Concatenative language

  • ACL
  • Ait
  • Aocla
  • Breeze
  • Callisto
  • Cat
  • Cognate
  • colorForth
  • Concata
  • CoSy
  • Deque
  • DSSP
  • dt
  • Elymas
  • Enchilada
  • ETAC
  • F
  • Factor
  • Fiveth
  • Forth
  • Fourth
  • Freelang
  • Gershwin
  • hex
  • iNet
  • Joy
  • Joy of Postfix App
  • kcats
  • Kitten
  • lang5
  • Listack
  • LSE64
  • Lviv
  • Meow5
  • min
  • Mirth
  • mjoy
  • Mlatu
  • Ode
  • OForth
  • Om
  • Onyx
  • Plorth
  • Popr
  • Porth
  • PostScript
  • Prowl
  • Quest32
  • Quackery
  • r3
  • Raven
  • Retro
  • RPL
  • SPL
  • Staapl
  • Stabel
  • Tal
  • Titan
  • Trith
  • Uiua
  • Worst
  • xs
  • XY
  • 5th
  • 8th

Concatenative topics

  • Compilers
  • Interpreters
  • Type systems
  • Object systems
  • Quotations
  • Variables
  • Garbage collection
  • Example programs

Concatenative meta

  • People
  • Communities

Other languages

  • APL
  • C++
  • Erlang
  • FP trivia
  • Haskell
  • Io
  • Java
  • JavaScript
  • Lisp
  • ML
  • Oberon
  • RPL
  • Self
  • Slate
  • Smalltalk

Meta

  • Search
  • Farkup wiki format
  • Etiquette
  • Sandbox

Sierpinski triangle

The Sierpinski triangle of order 4 should look like this:

                       *
                      * *
                     *   *
                    * * * *
                   *       *
                  * *     * *
                 *   *   *   *
                * * * * * * * *
               *               *
              * *             * *
             *   *           *   *
            * * * *         * * * *
           *       *       *       *
          * *     * *     * *     * *
         *   *   *   *   *   *   *   *
        * * * * * * * * * * * * * * * *

Uxntal

@on-reset ( -> )
	[ LIT &size 20 ] STHk #01 SUB
	&ver ( -- )
		DUP
		#00 EQUk ?{
			&pad ( -- )
				#2018 DEO
				INC GTHk ?&pad
		} POP 
		#00
		&fill
			ANDk #202a ROT ?{ SWP } POP #18 DEO
			#2018 DEO
			INC ADDk STHkr LTH ?&fill
		POP2
		#0a18 DEO
		#01 SUB INCk ?&ver
	POP POPr
	BRK

fy

:N 16;
N [ dup                         (y) 
    [' .c] dotimes              (print padding)
    N [ dup                     (x)
        over2 drop 1-           (y' = y - 1)
        swap N !-               (x' = N - x)
        &                       (x' & y')
        \'  \'* ifte .c ' .c ]  (print * or space)
    dotimes .nl]       
dotimes

Factor

: plot ( i j -- )
    bitand zero? "* " "  " ? write ;

: pad ( n -- ) 
    1 - [ bl ] times ;

: plot-row ( n -- ) 
    dup 1 + [ tuck - plot ] with each-integer ;

: sierpinski ( n -- )
    dup '[ _ over - pad plot-row nl ] each-integer ;

Kitten

vocab each_int:
    define helper<+P> (Int32, Int32, (Int32 -> +P) -> +P):
        -> n, i, f ;
        if (i < n):
            i f call
            n (i + 1) f helper
    
define each_int<+P> (Int32, (Int32 -> +P) -> +P):
    zero swap each_int::helper

define with_arg<R1..., R2..., T, A, B, S..., +P2, +P1>(R1..., T, B, (R2..., T, A -> S... +P2) -> R1..., B, (R2..., A -> S... +P1) +P1):
    {swap} dip {{swap} dip call} apply apply

define plot (Int32, Int32 -> +IO):
    (&) zero (=) if {"* "} else {"  "} print

define over<A, B> (A, B -> A, B, A):
    -> a, b; a b a

define tuck<A, B> (A, B -> B, A, B):
    swap over

define times<+P> (Int32, (-> +P) -> +P):
    {drop} swap compose each_int

define pad (Int32 -> +IO):
    1 (-) {" " print} times 

define plot_row (Int32 -> +IO):
    dup 1 (+) {tuck (-) plot} with_arg each_int

define sierpinski (Int32 -> +IO):
    dup {over (-) pad plot_row "\n" print} apply each_int

16 sierpinski

Forth

: star ( -- )     [char] * emit space ;
: plot ( i j -- ) and 0= if star else 2 spaces then ;
: padd ( n -- )   0 +do space loop ;
: 2^   ( n -- )   dup 0= if 1 else 1- 2 swap lshift then ;

: sierpinski ( o -- )
  2^ dup 1- -1 swap -do
    i padd dup 0 +do
      i j plot
    loop cr
  1 -loop ;

4 sierpinski

This revision created on Fri, 8 Mar 2024 23:08:30 by lobo (Add Forth version)

Latest Revisions Edit

All content is © 2008-2024 by its respective authors. By adding content to this wiki, you agree to release it under the BSD license.