Skip to content

fixes #835 #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions xml/chapter3/section3/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,52 @@ const put = operation_table("insert");
</SPLITINLINE>
for a local table.
<LABEL NAME="ex:numeric-keys"/>
<SOLUTION>
<SNIPPET>
<JAVASCRIPT>
// Solution by GitHub user clean99

function make_table(same_key) {
const local_table = list("*table*");
function assoc(key, records) {
return is_null(records)
? undefined
: same_key(key, head(head(records)))
? head(records)
: assoc(key, tail(records));
}
function lookup(key) {
const record = assoc(key, tail(local_table));
return is_undefined(record)
? undefined
: tail(record);
}
function insert(key, value) {
const record = assoc(key, tail(local_table));
if (is_undefined(record)) {
set_tail(local_table,
pair(pair(key, value), tail(local_table)));
} else {
set_tail(record, value);
}
return "ok";
}
function dispatch(m) {
return m === "lookup"
? lookup
: m === "insert"
? insert
: error(m, "unknow operation -- table");
}
return dispatch;
}

const operation_table = make_table((a, b) => a === b);
const get = operation_table("lookup");
const put = operation_table("insert");
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down
Loading