Funktionale Programmierung: Map, filter und List Comprehension |
|
|
|
1module Pythagoras where
2
3type Triangle = (Int, Int, Int)
4
5triples :: Int -> [Triangle]
6triples n
7 = [ (x, y, z) | x <- [1..n]
8 , y <- [x..n]
9 , z <- [y..n]
10 , x + y > z
11 ]
12
13triads :: Int -> [Triangle]
14triads n
15 = [ tri | tri <- triples n
16 , pyth tri
17 ]
18 where
19 pyth (x, y, z) = x*x + y*y == z*z
20
21triads' :: Int -> [Triangle]
22triads' n
23 = [ tri | tri <- triads n
24 , minimal tri
25 ]
26 where
27 minimal (x, y, z) = x `gcd` y `gcd` z == 1
|
|
Letzte Änderung: 20.09.2016 | © Prof. Dr. Uwe Schmidt |