Conca
Data
Conca knows about
- booleans,
- numbers,
- characters,
- strings, and
- lists.
Specify a character with a single quote, a string with double quotes, and a list with square brackets; the booleans are true and false. Strings and lists are indexed from 0 (zero).
Some special characters are
- \n newline
- \r return
- \t tab
- \b backspace
Words
Conca pushes data on the stack.
It executes words. Words operate on things stored in the stack.
Many words are pre-defined.
- clear (removes all from stack )
- . ( prints the top thing on the stack )
- top ( prints the top thing on the stack )
- top2 ( prints the top two things on the stack )
- top3 ( prints the top three things on the stack )
- drop ( remove top element from stack )
- drop2 ( remove top two elements from stack )
- drop3 ( remove top three elements from stack )
- pop ( item -- ; removes top of stack )
- dup ( item -- item item; duplicates top of stack )
- swap ( a b -- b a; swaps top two items )
- rotate ( a b c -- c b a )
- rollup ( a b c -- c a b )
- rolldown ( a b c -- b c a )
- dip ( quotation item -- item; save top, execute function, restore item )
- dip variants of previous five
- operators : + - * /
- arithmetic
- rem
- div
- abs
- trunc
- neg
- sign
- predicates
- boolean operators : and, or, not
- math
- power ( a b -- r; a raised to the power b )
- exp ( a -- r, e raised to the power a )
- sqrt, e, log, log10
- pi, sin, cos, tan, asin, acos, atan, atan2
Words which operate on aggregates: strings and lists.
- apply ( aggregate; execute the function in the aggregate )
- size ( aggregate -- aggregate n; pushes number of items in the aggregate )
- count ( aggregate -- n; pushes number of items in the aggregate and consumes the aggregate )
- at ( aggregate index -- r; element index of aggregate, zero based )
- of ( index aggregate -- r; element index of aggregate, zero based )
- cons ( aggregate atom -- aggregate; add atom to front of aggregate )
- uncons ( aggregate -- aggregate atom; split the first item from the rest of the aggregate )
- first ( aggregate -- atom; replace aggregate with its first item )
- rest ( aggregate -- aggregate; remove first item from aggregate )
- conca ( aggregate aggregate -- aggregate; concatenate the two aggregates )
- unit ( atom -- [ atom ]; make a list from the top iten on the stack )
- pair ( atom atom -- [ atom atom ]; make a list from the top two items on the stack )
- quote ( atom -- quotation; the same as unit )
- range ( start end -- aggregate; make an aggregate of numbers from start to end, step size is 1 )
- slice ( aggregate start end -- aggregate; extract slice from aggregate )
- reverse ( aggregate -- aggregate; reverse the other of the list or string )
Some control word.
- branch ( b [then] [else] -- ; if b is true, execute then, otherwise execute else )
- choice ( a b c -> if a is true, then b, else c; it chooses a value based on a boolean)
- ift ( [test] [then]; if test returns true, execute then, otherwise do nothing )
- ifte ( [test] [then] [else]; if test returns true, execute then, otherwise execute else )
- integer? ( thing -- boolean; true if a in an integer, else otherwise )
- float? ( thing -- boolean; true if a in a float, else otherwise )
- list? ( thing -- boolean; true if a in a list, else otherwise )
- string? ( thing -- boolean; true if a in a string, else otherwise )
- while ( [test] [loop]; execute loop as long as test returns true )
- times ( n [loop]; executes loop n times )
- map ( aggregate quotation; apply the quotation to every item in the aggregate and put the on the stack in an aggregate, the function in the quotation must consume one item and produce one item )
- foldr ( aggregate start-value quotation -- result; apply the quotation which consumes two things to the start-value and the current front of the aggregate, consume the whole aggregate and leave the result )
- foldl ( aggregate start-value quotation -- result; apply the quotation which consumes two things to the start-value and the current end of the aggregate, consume the whole aggregate and leave the result )
Note : foldr and foldl with produce identical results when the quotation is commutative.
Some examples
>> 4 ["times\n" print] times
times
times
times
times
>> 3 [1 - dup 0 >=] ["counting\n" print] while
counting
counting
counting
( Greatest common devisor, yes this is a comment, nestable )
>> 144 124 [dup 0 >] [dup rollup rem] while pop .
4
>> "abc" [upcase] map .
"ABC"
>> [1 2 3] [2 *] map .
[ 2 4 6 ]
You can define new words like this:
quotation "name" define ( a quotation is a list I will execute )
Example:
[ dup * ] "square" define
You can now do this:
111111111 square .
12345678987654321
Load code form a file:
Factorial
[1 2 3 4 5] 1 [*] foldr .
[ "this is" " a " "good idea"] "" [conca] foldr .
Try it with foldl.
You may notice that numbers are unusual. There are