From a theoritecal point of view, if a computational problem requires an input and an output without side-effect, lambda calculus can probably solve it (more generally, lambda calculus is Turing complete, cf wikipedia).
Now for the implementation, the following lambda
function takes a list argument, and returns a list where all the duplicates have been removed:
lambda l: (lambda u, a: u(u, a)) ((lambda f, x: x if len(x) <= 0 else (f(f, x[1:]) if x[0] in x[1:] else ([x[0]] + f(f, x[1:])))), l)
Here is an unwrapped version:
lambda l:
(lambda u, a: u(u, a))
(
(lambda f, x: x if len(x) <= 0
else
(
f(f, x[1:]) if x[0] in x[1:]
else ([x[0]] + f(f, x[1:]))
)
),
l
)
The function consists in a lambda
version of the following recursive function:
def f(l):
if len(l) <= 0:
return l
elif l[0] in l[1:]:
return f(l[1:])
else:
return ([l[0]] + f(l[1:]))
To emulate a recursive call, the equivalent lambda
takes an additional function as argument, that will be itself:
lambda f, x: x if len(x) <= 0
else
(
f(f, x[1:]) if x[0] in x[1:]
else ([x[0]] + f(f, x[1:]))
)
Then, another lambda
calls this previous function, passing itself as argument (besides the list):
lambda u, a: u(u, a)
Finally, an outer lambda
wraps everything up, that takes only a list as argument.