Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created July 27, 2010 15:21
Show Gist options
  • Select an option

  • Save Koitaro/492368 to your computer and use it in GitHub Desktop.

Select an option

Save Koitaro/492368 to your computer and use it in GitHub Desktop.
Pythagoreans
definition module Pythagoreans
:: ListTree a = Node a [ListTree a]
primitivePythagoreanTree :: ListTree [Int]
pythagoreans :: ([Int] -> Bool) -> [[Int]]
implementation module Pythagoreans
import StdEnv
primitivePythagoreanTree :: ListTree [Int]
primitivePythagoreanTree =: f [3,4,5] where
f xs = Node xs ((map f o gen) xs)
gen [x0,x1,x2] = map (\x = map (sumProd x) P) xs where
P = [[-1,-2,2],[-2,-1,2],[-2,-2,3]]
xs = [[~x0,x1,x2],[x0,~x1,x2],[~x0,~x1,x2]]
sumProd xs ys = sum [x*y \\ x <- xs & y <- ys]
pythagoreans :: ([Int] -> Bool) -> [[Int]]
pythagoreans cond = (flatten o map f) ps where
f ps = takeWhile cond [map ((*) x) ps \\ x <- [1..]]
ps = f primitivePythagoreanTree where
f (Node xs ts)
| cond xs = [xs:(flatten o map f) ts]
| otherwise = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment