List Contains Element Recursive Property

If the first element of the list is not equal to the value you are looking for, then see whether the rest of the list contains the value to determine whether the list contains the value. More formally,

if not (x = value), then stack [ x, y ] contains value = stack y contains value

For example, consider the following:

if not (2 = 4), then stack [ 2, [ 4, [ 6, [ ] ] ] ] contains 4 = stack [ 4, [ 6, [ ] ] ] contains 4

We want to know whether the list [2,[4,[6,[]]]] has 4. The first element, 2, is not equal to 4. Then, the following question:

Does the list [2,[4,[6,[]]]] contain 4?

Becomes the same as this:

Does the list [4,[6,[]]] contain 4?

Since [4,[6,[]]] contains 4, so does [2,[4,[6,[]]]].

Here is another example:

if not (2 = 100), then stack [ 2, [ 4, [ 6, [ ] ] ] ] contains 100 = stack [ 4, [ 6, [ ] ] ] contains 100

We want to know whether the list [2,[4,[6,[]]]] contains 100. The first element, 2, is not equal to 100. Then, the following question:

Does the list [2,[4,[6,[]]]] contain 100?

Becomes the same as this:

Does the list [4,[6,[]]] contain 100?

The first element, 4, is again not equal to 100. Then the above question becomes the same as:

Does the list [6,[]] contain 100?

The first element, 6, is again not equal to 100. There are no more elements in this list, so we conclude that the list [6, []] does not contain 100.

Since [6, []] does not contain 100, we also conclude that [4, [6, []]] does not contain 100, and finally conclude that [2,[4,[6,[]]]] does not contain 100.

Recursively looking for an element feels inefficient since it forces us to only consider the first element at all times, but this allows us to define one property for lists of all sizes.

Examples

if not (5 = 10), then stack [ 5, [ 10, [ 15, [ ] ] ] ] contains 10 = stack [ 10, [ 15, [ ] ] ] contains 10

if not ("Apple" = "Mango"), then stack [ "Apple", [ "Banana", [ "Cherry", [ ] ] ] ] contains "Mango" = stack [ "Banana", [ "Cherry", [ ] ] ] contains "Mango"

if not (True = False), then stack [ True, [ False, [ ] ] ] contains False = stack [ False, [ ] ] contains False


Comments

Please log in to add comments