Skip to content

Instantly share code, notes, and snippets.

@nunorc
Created August 31, 2010 13:16
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 nunorc/558997 to your computer and use it in GitHub Desktop.
Save nunorc/558997 to your computer and use it in GitHub Desktop.
Create a list of four times 1:
Main> flip replicate 1 4
[1,1,1,1]
Now do it for every element of the list and get a list of lists:
Main> map (flip replicate 1) [4,1,3,2]
[[1,1,1,1],[1],[1,1,1],[1,1]]
Transpose that twice to get the inner lists sorted by number of elements:
Main> transpose(transpose(map (flip replicate 1) [4,1,3,2]))
[[1,1,1,1],[1,1,1],[1,1],[1]]
Now for every element of that list sum the number of elements in the inner list
Main> map sum (transpose(transpose(map (flip replicate 1) [4,1,3,2])))
[4,3,2,1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment