Skip to content

Commit

Permalink
establish vocabulary for a few more sections
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfitz committed Oct 5, 2019
1 parent c77aa66 commit 57c3d1e
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 194 deletions.
16 changes: 16 additions & 0 deletions msg/COS_Begin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

const cos = require('./cosmic');

cos.header('MATH', 'show how to execute a sequence of instructions');

cos.add(`intro begin`);
cos.add(`define translate:begin:prev $translate`);
cos.add(`define translate | let ((prev $translate:begin:prev)) | ? x |
if (not | function? $x) (prev $x) |
if (not | = (head $x) begin) (prev $x) |
translate | vector (vector ? x (vector last (vector x))) (prepend vector | tail $x)`);
cos.add(`= 4 | begin 1 7 2 4`);
cos.add(`= 6 | begin (set! $demo:make-cell:x 88) (set! $demo:make-cell:x 6) (get! $demo:make-cell:x)`);
cos.add(`= 88 | begin (set! $demo:make-cell:y 88) (set! $demo:make-cell:x 6) (get! $demo:make-cell:y)`);
cos.add(`= 4 | begin (set! $demo:make-cell:x 88) (set! $demo:make-cell:x 6) (get! $demo:make-cell:x) 4`);
80 changes: 0 additions & 80 deletions msg/COS_Begin.pl

This file was deleted.

32 changes: 32 additions & 0 deletions msg/COS_Graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

const cos = require('./cosmic');

cos.seed(42);

cos.add(`define graph:make | lambda (x:graph:1 x:graph:2) | pair $x:graph:1 $x:graph:2`);

cos.add(`define demo:graph | graph:make
(vector 1 2 3 4)
(vector (pair 1 2) (pair 2 3) (pair 1 4))`);

cos.add(`define exists:graph:2 | lambda (x:graph y:graph:1 z:graph:1) |
exists | ? n |
if (or (< $n 0) (>= $n | list-length | list-ref $x:graph 1)) $false |
list= (list-ref (list-ref $x:graph 1) $n) (pair $y:graph:1 $z:graph:1)`);

cos.add(`exists:graph:2 $demo:graph 1 2`);
cos.add(`not | exists:graph:2 $demo:graph 1 3`);
cos.add(`not | exists:graph:2 $demo:graph 2 4`);
cos.add(`exists:graph:2 $demo:graph 1 4`);

cos.add(`define exists:graph:2:list | lambda (x:graph y:graph:1 z:graph:1) |
if (= $y:graph:1 $z:graph:1) $true |
if (exists:graph:2 $x:graph $y:graph:1 $z:graph:1) $true |
exists | ? n:graph:1 |
if (not | exists:graph:2 $x:graph $y:graph:1 $n:graph:1) $false |
exists:graph:2:list $x:graph $n:graph:1 $z:graph:1`);

cos.add(`exists:graph:2:list $demo:graph 1 2`);
cos.add(`exists:graph:2:list $demo:graph 1 3`);
cos.add(`not | exists:graph:2:list $demo:graph 2 4`);
2 changes: 2 additions & 0 deletions msg/COS_Graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# MATH introduce graph structures

100 changes: 0 additions & 100 deletions msg/COS_Graph.pl

This file was deleted.

22 changes: 11 additions & 11 deletions msg/COS_Hash.pl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ sub ShowHashLesson {
$txt .= "# note that at the time of writing (h 1 2) is same as ((h) 1 2)\n";
$txt .= ShowLine(Op2("define",
"hash-add",
ProcMultiple(["h","x","y","z"],
ProcMultiple(["x:hash","x","y","z"],
Op("if",
Op2("equal",Ref("z"),Ref("x")),
Ref("y"),
Op("h", Ref("z"))))));
Op("x:hash", Ref("z"))))));
$txt .= ShowLine(Op2("define",
"hash-ref",
ProcMultiple(["h","x"],
Op("h", Ref("x")))));
ProcMultiple(["x:hash","x"],
Op("x:hash", Ref("x")))));

$txt .= ShowLine(Op2("define",
"hash-null",
Expand All @@ -30,7 +30,7 @@ sub ShowHashLesson {
Proc("x",Ref("default")))));

$txt .= ShowLine(Op2("define",
"test-hash",
"demo:hash",
Op("hash-add",
Op("hash-add",
Ref("hash-null"),
Expand All @@ -40,40 +40,40 @@ sub ShowHashLesson {

$txt .= ShowLine(Op2("=",
Op("hash-ref",
Ref("test-hash"),
Ref("demo:hash"),
4),
9));

$txt .= ShowLine(Op2("=",
Op("hash-ref",
Ref("test-hash"),
Ref("demo:hash"),
3),
2));

$txt .= ShowLine(Op2("=",
Op("hash-ref",
Ref("test-hash"),
Ref("demo:hash"),
8),
Ref("undefined")));

$txt .= ShowLine(Op2("=",
Op("hash-ref",
Ref("test-hash"),
Ref("demo:hash"),
15),
Ref("undefined")));

$txt .= ShowLine(Op2("=",
Op("hash-ref",
Op("hash-add",
Ref("test-hash"),
Ref("demo:hash"),
15,
33),
15),
33));

$txt .= ShowLine(Op2("=",
Op("hash-ref",
Ref("test-hash"),
Ref("demo:hash"),
15),
Ref("undefined")));

Expand Down
8 changes: 8 additions & 0 deletions msg/COS_List.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ define except-last | ? x |
prepend (head $x) | except-last | tail $x`);
cos.add("= 15 | last | vector 4 5 15");
cos.add("list= (vector 4 5) | except-last | vector 4 5 15");

cos.add(`intro list:reverse`);
cos.add(`define list:reverse | ? x:list |
if (<= (list-length $x:list) 1) $x:list |
prepend (last $x:list) | list:reverse | except-last $x:list`);

cos.add(`list= (list:reverse | vector 1 2 3) (vector 3 2 1)`);
cos.add(`list= (list:reverse | vector 50 1 33 99) (vector 99 33 1 50)`);
3 changes: 3 additions & 0 deletions msg/cosmic.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,8 @@ CosWrite.prototype.vector = function(lst,wrap) {
return result;
}

CosWrite.prototype.header = function(section, header) {
console.log(`# ${section} ${header}`);
}

module.exports = new CosWrite();
Loading

0 comments on commit 57c3d1e

Please sign in to comment.