unique (nub)

The 'unique' (nub) function removes duplicate elements from a list while preserving the order of first occurrence. It operates correctly on even infinite (lazy) lists.

Note that 'r' is a list and 'u' is a function and that they have mutually recursive definitions [All89] – r depends on u and u depends on r. Bird called programs with self-referential data-structures circular programs.




References

[All89] Lloyd Allison, 'Circular Programs and Self-Referential Structures', Software Practice & Experience, 19(2), pp.99-109, doi:10.1002/spe.4380190202, arxiv:2403.01866, February 1989.
[All93] Lloyd Allison, 'Applications of Recursively Defined Data Structures', Australian Computer Journal, 25(1), pp.14-20, arxiv:2206.12795, 1993.
And other publications.

There are more λ-calculus examples here.