@@ -1522,6 +1522,56 @@ display(count_pairs(cycle));
1522
1522
distinct pairs in any structure. (Hint: Traverse the structure, maintaining
1523
1523
an auxiliary data structure that is used to keep track of which pairs have
1524
1524
already been counted.)
1525
+ <SOLUTION >
1526
+ <SNIPPET >
1527
+ <EXAMPLE >exercise_3_17_solution_example</EXAMPLE >
1528
+ <JAVASCRIPT >
1529
+ // solution provided by GitHub user clean99
1530
+
1531
+ function count_pairs(x) {
1532
+ let counted_pairs = null;
1533
+ function is_counted_pair(current_counted_pairs, x) {
1534
+ return is_null(current_counted_pairs)
1535
+ ? false
1536
+ : head(current_counted_pairs) === x
1537
+ ? true
1538
+ : is_counted_pair(tail(current_counted_pairs), x);
1539
+ }
1540
+ function count(x) {
1541
+ if(! is_pair(x) || is_counted_pair(counted_pairs, x)) {
1542
+ return 0;
1543
+ } else {
1544
+ counted_pairs = pair(x, counted_pairs);
1545
+ return count(head(x)) +
1546
+ count(tail(x)) +
1547
+ 1;
1548
+ }
1549
+ }
1550
+ return count(x);
1551
+ }
1552
+ </JAVASCRIPT >
1553
+ </SNIPPET >
1554
+ <SNIPPET HIDE =" yes" >
1555
+ <NAME >exercise_3_17_solution_example</NAME >
1556
+ <JAVASCRIPT >
1557
+ const three_list = list("a", "b", "c");
1558
+ const one = pair("d", "e");
1559
+ const two = pair(one, one);
1560
+ const four_list = pair(two, "f");
1561
+ const seven_list = pair(two, two);
1562
+ const cycle = list("g", "h", "i");
1563
+ set_tail(tail(tail(cycle)), cycle);
1564
+
1565
+ // return 3; return 3; return 3;
1566
+ display(count_pairs(three_list));
1567
+ display(count_pairs(four_list));
1568
+ display(count_pairs(seven_list));
1569
+
1570
+ // return 3
1571
+ display(count_pairs(cycle));
1572
+ </JAVASCRIPT >
1573
+ </SNIPPET >
1574
+ </SOLUTION >
1525
1575
</EXERCISE >
1526
1576
1527
1577
<EXERCISE >
@@ -1542,6 +1592,50 @@ display(count_pairs(cycle));
1542
1592
would go into an infinite loop. Exercise<SPACE /><REF NAME =" ex:make-cycle" />
1543
1593
constructed such lists.
1544
1594
<LABEL NAME =" ex:find-cycle" />
1595
+ <SOLUTION >
1596
+ <SNIPPET >
1597
+ <EXAMPLE >exercise_3_18_solution_example</EXAMPLE >
1598
+ <JAVASCRIPT >
1599
+ // solution provided by GitHub user clean99
1600
+
1601
+ function contains_cycle(x) {
1602
+ let counted_pairs = null;
1603
+ function is_counted_pair(counted_pairs, x) {
1604
+ return is_null(counted_pairs)
1605
+ ? false
1606
+ : head(counted_pairs) === x
1607
+ ? true
1608
+ : is_counted_pair(tail(counted_pairs), x);
1609
+ }
1610
+ function detect_cycle(x) {
1611
+ if (is_null(x)) {
1612
+ return false;
1613
+ } else if (is_counted_pair(counted_pairs, x)) {
1614
+ return true;
1615
+ } else {
1616
+ counted_pairs = pair(x, counted_pairs);
1617
+ return detect_cycle(tail(x));
1618
+ }
1619
+ }
1620
+ return detect_cycle(x);
1621
+ }
1622
+ </JAVASCRIPT >
1623
+ </SNIPPET >
1624
+ <SNIPPET HIDE =" yes" >
1625
+ <NAME >exercise_3_18_solution_example</NAME >
1626
+ <JAVASCRIPT >
1627
+ const three_list = list("a", "b", "c");
1628
+ const cycle = list("g", "h", "i");
1629
+ set_tail(tail(tail(cycle)), cycle);
1630
+
1631
+ // displays false
1632
+ display(contains_cycle(three_list));
1633
+
1634
+ // displays true
1635
+ display(contains_cycle(cycle));
1636
+ </JAVASCRIPT >
1637
+ </SNIPPET >
1638
+ </SOLUTION >
1545
1639
</EXERCISE >
1546
1640
1547
1641
<EXERCISE >
0 commit comments