top | item 42042423

(no title)

hazbo | 1 year ago

I remember my mind being blown when I first came across list comprehensions. It was one thing to be able to effectively generate one list from another:

  Squares = [X*X || X <- [1,2,3,4]].
(This can be read as, Squares equals a list of X*Xs such that each X is a member of [1,2,3,4])

Going from that to then realizing that you can use list comprehensions to implement quicksort in basically 2 lines of code:

  qsort([]) ->
     [];
  qsort([H | T]) ->
     qsort([ X || X <- T, X < H ]) ++ [H] ++ qsort([ X || X <- T, X >= H ]).
These examples are written in Erlang though list comprehensions are found in many other languages, Python and Haskell to name a couple.

discuss

order

No comments yet.