mirror of
https://github.com/antirez/aocla
synced 2024-12-27 09:58:32 +01:00
Add a few example programs.
This commit is contained in:
parent
ea3886d0cb
commit
965ab21953
4 changed files with 47 additions and 1 deletions
2
aocla.c
2
aocla.c
|
@ -1047,7 +1047,7 @@ void loadLibrary(aoclactx *ctx) {
|
||||||
addProcString(ctx,"drop","[(_)]");
|
addProcString(ctx,"drop","[(_)]");
|
||||||
|
|
||||||
/* [1 2 3] [dup *] map => [1 4 9] */
|
/* [1 2 3] [dup *] map => [1 4 9] */
|
||||||
addProcString(ctx,"map", "[(l f) $l len (e) 0 (j) [] [$j $e <] [ $l $j get@ $f upeval swap -> $j 1 + (j)] while]");
|
addProcString(ctx,"map", "[(l f) $l len (e) 0 (j) [] [$j $e <] [$l $j get@ $f upeval swap -> $j 1 + (j)] while]");
|
||||||
|
|
||||||
/* [1 2 3] [printnl] foreach */
|
/* [1 2 3] [printnl] foreach */
|
||||||
addProcString(ctx,"foreach"," [(l f) $l len (e) 0 (j) [$j $e <] [$l $j get@ $f upeval $j 1 + (j)] while]");
|
addProcString(ctx,"foreach"," [(l f) $l len (e) 0 (j) [$j $e <] [$l $j get@ $f upeval $j 1 + (j)] while]");
|
||||||
|
|
11
examples/cat.aocla
Normal file
11
examples/cat.aocla
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
// List concatenation. This procedure is part of the standard
|
||||||
|
// library.
|
||||||
|
|
||||||
|
// [1 2 3] [4 5 6] cat => [1 2 3 4 5 6]
|
||||||
|
[(a b)
|
||||||
|
$b [$a -> (a)] foreach
|
||||||
|
$a
|
||||||
|
] 'cat def
|
||||||
|
|
||||||
|
[1 2 3] [4 5 6] cat
|
||||||
|
showstack
|
21
examples/firstrest.aocla
Normal file
21
examples/firstrest.aocla
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Fundamental list manipulation functions. Part of the standard library.
|
||||||
|
// first, rest, cat
|
||||||
|
|
||||||
|
[
|
||||||
|
0 get@
|
||||||
|
] 'first def
|
||||||
|
|
||||||
|
[
|
||||||
|
#t (f) // True only for the first element
|
||||||
|
[] (n) // New list
|
||||||
|
[
|
||||||
|
[$f] [
|
||||||
|
#f (f) // Set flag to false
|
||||||
|
drop // Discard first element
|
||||||
|
] [
|
||||||
|
$n -> (n)
|
||||||
|
] ifelse
|
||||||
|
] foreach
|
||||||
|
$n
|
||||||
|
] 'rest def
|
||||||
|
|
14
examples/foreach.aocla
Normal file
14
examples/foreach.aocla
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// This is a commented version of the implementation of 'foreach' inside
|
||||||
|
// the standard library.
|
||||||
|
|
||||||
|
[(l f) // list and function to call with each element.
|
||||||
|
$l len (e) // Get list len in "e"
|
||||||
|
0 (j) // j is our current index
|
||||||
|
[$j $e <] [
|
||||||
|
$l $j get@ // Get list[j]
|
||||||
|
$f upeval // We want to evaluate in the context of the caller
|
||||||
|
$j 1 + (j) // Go to the next index
|
||||||
|
] while
|
||||||
|
] 'foreach def
|
||||||
|
|
||||||
|
[1 2 3] [printnl] foreach
|
Loading…
Reference in a new issue