Skip to content

Commit e055d87

Browse files
authored
fixes #835 (#1022)
1 parent f243b1e commit e055d87

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

xml/chapter3/section3/subsection3.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,52 @@ const put = operation_table("insert");
666666
</SPLITINLINE>
667667
for a local table.
668668
<LABEL NAME="ex:numeric-keys"/>
669+
<SOLUTION>
670+
<SNIPPET>
671+
<JAVASCRIPT>
672+
// Solution by GitHub user clean99
673+
674+
function make_table(same_key) {
675+
const local_table = list("*table*");
676+
function assoc(key, records) {
677+
return is_null(records)
678+
? undefined
679+
: same_key(key, head(head(records)))
680+
? head(records)
681+
: assoc(key, tail(records));
682+
}
683+
function lookup(key) {
684+
const record = assoc(key, tail(local_table));
685+
return is_undefined(record)
686+
? undefined
687+
: tail(record);
688+
}
689+
function insert(key, value) {
690+
const record = assoc(key, tail(local_table));
691+
if (is_undefined(record)) {
692+
set_tail(local_table,
693+
pair(pair(key, value), tail(local_table)));
694+
} else {
695+
set_tail(record, value);
696+
}
697+
return "ok";
698+
}
699+
function dispatch(m) {
700+
return m === "lookup"
701+
? lookup
702+
: m === "insert"
703+
? insert
704+
: error(m, "unknow operation -- table");
705+
}
706+
return dispatch;
707+
}
708+
709+
const operation_table = make_table((a, b) => a === b);
710+
const get = operation_table("lookup");
711+
const put = operation_table("insert");
712+
</JAVASCRIPT>
713+
</SNIPPET>
714+
</SOLUTION>
669715
</EXERCISE>
670716

671717
<EXERCISE>

0 commit comments

Comments
 (0)