SECTION 24 / 24

Tutorial: rest, init and membership

2 min read
Procedure rest(L):
    Found = false
    Result = []
    foreach x in L:
        if Found:
            Result = Result ++ [x]
        else:
            Found = true
    return Result

Procedure init(L):
    Found = false
    Result = []
    foreach x in L:
        if Found:
            Result = Result ++ [Prev]
        else:
            Found = true
        Prev = x
    return Result

rest uses Found to skip exactly the first occurrence. init uses a one-element delay: when the second element arrives, append the remembered first; when the third arrives, append the remembered second. Update Prev AFTER appending it. The final current element is never appended, so the final position is excluded.

Both functions return [] on [] and on a singleton under the tutorial’s definitions. “L minus first(L)” is an informal description of dropping one endpoint, not a set subtraction that deletes every equal value. The functions build new result lists; the caller chooses whether to assign the result back to its working variable.

member(L,e) returns true after a match and false only after exhausting the list. Its parameter order is list first, element second.
member(L,e) returns true after a match and false only after exhausting the list. Its parameter order is list first, element second.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Search for an element in a list

Follow one occurrence at a time. Choose another input to test empty lists, repeated IDs and different parameters.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change, then step through the list. The original course records remain unchanged.

Procedure member(L, e):
    foreach x in L:
        if e == x:
            return true
    return false

A true result can return early; a false result comes after the loop. The empty list returns false with zero equality tests. Repeated values do not require repeated true results: membership asks only whether at least one occurrence exists. Parameter order is member(list,element), matching the tutorial.

The tutorial’s length and last scan the list; rest and init copy result elements. Repeatedly calling these scanning or copying helpers inside another loop can add work. A short pseudocode expression is not an automatic constant-time guarantee. At this stage, first understand the interface and result; later analyze the chosen implementation’s cost.

Try it yourself

In init, what goes wrong if Prev = x happens before appending Prev?

Show explained answer

You append the current element rather than the previous one, shifting the result and potentially retaining the final element.

Try it yourself

Why is return false inside member’s loop after the first unequal value wrong?

Show explained answer

A later occurrence can still match. False requires finishing the search; true needs only one match.

Try it yourself

For rest([3,3,25]) and init([3,3,25]), what are the results?

Show explained answer

rest gives [3,25]; init gives [3,3]. They remove positions at opposite ends, not values everywhere.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑