File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -666,6 +666,52 @@ const put = operation_table("insert");
666
666
</SPLITINLINE >
667
667
for a local table.
668
668
<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 >
669
715
</EXERCISE >
670
716
671
717
<EXERCISE >
You can’t perform that action at this time.
0 commit comments