Created
July 27, 2010 15:21
-
-
Save Koitaro/492368 to your computer and use it in GitHub Desktop.
Pythagoreans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| definition module Pythagoreans | |
| :: ListTree a = Node a [ListTree a] | |
| primitivePythagoreanTree :: ListTree [Int] | |
| pythagoreans :: ([Int] -> Bool) -> [[Int]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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