Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Created April 29, 2011 19:16
Show Gist options
  • Save fearofcode/948838 to your computer and use it in GitHub Desktop.
Save fearofcode/948838 to your computer and use it in GitHub Desktop.
haskell is pretty cool
import Data.List
rightTriangles = [(a,b,c) | c <- [1 .. ], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
{--
*Main> take 10 rightTriangles
[(3,4,5),(6,8,10),(5,12,13),(9,12,15),(8,15,17),(12,16,20),(15,20,25),(7,24,25),(10,24,26),(20,21,29)]
--}
countOccurrences :: (Ord a) => [a] -> [(a, Int)]
countOccurrences = map (\l@(x:xs) -> (x, length l)) . group . sort
{--
*Main Data.List> countOccurrences [1, 2, 3, 4, 5, 1]
[(1,2),(2,1),(3,1),(4,1),(5,1)]
--}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment