Skip to content

Fix to insert in the Solution to Exercise 3.26 #1146

@KrNor

Description

@KrNor

In the current solution to Exercise 3.26 the insert function does nothing when a matching key is found in the table.

The issue raised with the solution to this Exercise included the logic for this. #805

But in the commit 245bdc0 some changes were made, and it was not added.


From my understanding. In the original issue with the solution, when a matching key is found, the mutation of the value caused the structure of the tree to change.


...
put(3, "d");
put(1, "a");
put(2, "b");

Image

Calling get(2) returns: [2, ["b", null]]

put(2, "c");

Image

Now calling get(2) returns: [2, "c"]

And changing the get function to only return the value, caused errors.


To add the functionality of mutating a value, to the current solution, we need to change: (245bdc0#diff-2fcf2160827ca147db35db6ff4fb66b54efe0be937c726b2b87cedd80d4f1dc1R875)
from:

...
function insert(k, v) {
    let record = lookup(k, local_table);
    if (is_null(record)) {
    local_table = adjoin_set(list(k, v), local_table);
    } else {
    // do nothing
    }
}
...

to:

...
function insert(k, v) {
  let record = lookup(k, local_table);
  if (is_null(record)) {
    local_table = adjoin_set(list(k, v), local_table);
  } else {
    set_tail(record, list(v));
  }
}
...

And the comment can be removed :
(245bdc0#diff-2fcf2160827ca147db35db6ff4fb66b54efe0be937c726b2b87cedd80d4f1dc1R921 )

...
display(get(2)); // displays: "b"
...

to:

...
display(get(2));
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions