Skip to content

Commit

Permalink
fixes #825 (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-henz authored Jul 7, 2024
1 parent 272478a commit 3845699
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions xml/chapter3/section3/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,50 @@ display(count_pairs(cycle));
would go into an infinite loop. Exercise<SPACE/><REF NAME="ex:make-cycle"/>
constructed such lists.
<LABEL NAME="ex:find-cycle"/>
<SOLUTION>
<SNIPPET>
<EXAMPLE>exercise_3_18_solution_example</EXAMPLE>
<JAVASCRIPT>
// solution provided by GitHub user clean99

function contains_cycle(x) {
let counted_pairs = null;
function is_counted_pair(counted_pairs, x) {
return is_null(counted_pairs)
? false
: head(counted_pairs) === x
? true
: is_counted_pair(tail(counted_pairs), x);
}
function detect_cycle(x) {
if (is_null(x)) {
return false;
} else if (is_counted_pair(counted_pairs, x)) {
return true;
} else {
counted_pairs = pair(x, counted_pairs);
return detect_cycle(tail(x));
}
}
return detect_cycle(x);
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>exercise_3_18_solution_example</NAME>
<JAVASCRIPT>
const three_list = list("a", "b", "c");
const cycle = list("g", "h", "i");
set_tail(tail(tail(cycle)), cycle);

// displays false
display(contains_cycle(three_list));

// displays true
display(contains_cycle(cycle));
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down

0 comments on commit 3845699

Please sign in to comment.