UP | HOME

Pythagorean triples computation

This is a beautiful example of List monad

import Control.Monad

triples :: [(Int, Int, Int)]
triples = do
  z <- [1 ..]
  x <- [1 .. z]
  y <- [x .. z]
  guard (x ^ 2 + y ^ 2 == z ^ 2)
  pure (x, y, z)
Date: 2023-04-16 Sun 16:57
Author: Lîm Tsú-thuàn