Haskell is kind of terse

Prelude> -- Hey Haskell! Show me the distribution of totals of 3 six sided dice.
Prelude> import Data.List
Prelude Data.List> [ (head w, length w) | w < - (group $ sort [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6] ])]
[(3,1),(4,3),(5,6),(6,10),(7,15),(8,21),(9,25),(10,27),(11,27),(12,25),(13,21),(14,15),(15,10),(16,6),(17,3),(18,1)]
Prelude Data.List>

So where’d that come from?

Here is how that one liner was built up bit by bit…


Prelude Data.List> Prelude Data.List> [1..6]
[1,2,3,4,5,6]
Prelude Data.List> [ x | x <- [1..6]]
[1,2,3,4,5,6]
Prelude Data.List> [ x + y | x <- [1..6], y <- [1..6]]
[2,3,4,5,6,7,3,4,5,6,7,8,4,5,6,7,8,9,5,6,7,8,9,10,6,7,8,9,10,11,7,8,9,10,11,12]
Prelude Data.List> [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6]]
[3,4,5,6,7,8,4,5,6,7,8,9,5,6,7,8,9,10,6,7,8,9,10,11,7,8,9,10,11,12,8,9,10,11,12,13,4,5,6,7,8,9,5,6,7,8,9,10,6,7,8,9,10,11,7,8,9,10,11,12,8,9,10,11,12,13,9,10,11,12,13,14,5,6,7,8,9,10,6,7,8,9,10,11,7,8,9,10,11,12,8,9,10,11,12,13,9,10,11,12,13,14,10,11,12,13,14,15,6,7,8,9,10,11,7,8,9,10,11,12,8,9,10,11,12,13,9,10,11,12,13,14,10,11,12,13,14,15,11,12,13,14,15,16,7,8,9,10,11,12,8,9,10,11,12,13,9,10,11,12,13,14,10,11,12,13,14,15,11,12,13,14,15,16,12,13,14,15,16,17,8,9,10,11,12,13,9,10,11,12,13,14,10,11,12,13,14,15,11,12,13,14,15,16,12,13,14,15,16,17,13,14,15,16,17,18]
Prelude Data.List> sort [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6]]
[3,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,17,17,17,18]
Prelude Data.List> group $ sort [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6]]
[[3],[4,4,4],[5,5,5,5,5,5],[6,6,6,6,6,6,6,6,6,6],[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7],[8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8],[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9],[10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],[11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11],[12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12],[13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13],[14,14,14,14,14,14,14,14,14,14,14,14,14,14,14],[15,15,15,15,15,15,15,15,15,15],[16,16,16,16,16,16],[17,17,17],[18]]
Prelude Data.List> [ length w | w <- group $ sort [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6]]]
[1,3,6,10,15,21,25,27,27,25,21,15,10,6,3,1]
Prelude Data.List> [ head w | w <- group $ sort [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6]]]
[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
Prelude Data.List> [ (head w, length w) | w <- group $ sort [ x + y + z | x <- [1..6], y <- [1..6], z <- [1..6]]]
[(3,1),(4,3),(5,6),(6,10),(7,15),(8,21),(9,25),(10,27),(11,27),(12,25),(13,21),(14,15),(15,10),(16,6),(17,3),(18,1)]
Prelude Data.List>