150k views
5 votes
A functional language could theoretically use any data structure other than a list.

Describe the purpose of the CDR function for a list. What function would achieve a similar for each of

the following data structures?

• Stacks

• 2D Tables

• Trees

• Graphs

User Jooin
by
8.7k points

1 Answer

4 votes

Answer:

In a list, the `cdr` function returns the second element of a pair. It is often used to traverse through a list by returning the tail of the list, which is the remainder of the list after the head element.

For stacks, a similar function to `cdr` would be `pop`, which removes and returns the top element of the stack.

For 2D tables, a similar function to `cdr` would be `tail`, which returns the remaining rows of the table after the first row.

For trees, a similar function to `cdr` would be `right`, which returns the right subtree of a node.

For graphs, a similar function to `cdr` would be `neighbors`, which returns the adjacent nodes of a node.

User Martin Lund
by
8.8k points