Skip to content

Instantly share code, notes, and snippets.

@wolph
Created August 19, 2015 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/17552c9508753044e44f to your computer and use it in GitHub Desktop.
Save wolph/17552c9508753044e44f to your computer and use it in GitHub Desktop.
Quicksort using the Y Combinator (lambda calculus) in pure Python
# Quicksort using the Y Combinator (lambda calculus) in pure Python
>>> Y = lambda f: lambda *args: f(Y(f))(*args)
>>> quicksort = Y(lambda f:
... lambda x: (
... f([item for item in x if item < x[0]])
... + [y for y in x if x[0] == y]
... + f([item for item in x if item > x[0]])
... ) if x else [])
>>> quicksort([1, 3, 5, 4, 1, 3, 2])
[1, 1, 2, 3, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment