-- einfaches Beispiel für Parallelisierung: -- (Berechnung von Binomialkoeffizienten) ----------------------------------------- import System(getArgs) -- Argumente lesen import Parallel -- Lib. für GpH main :: IO () main = do args <- getArgs putStrLn ("Binomialkoeffizient "++ args!!0 ++ " ueber " ++ args!!1 ++ ": ") let a = (read (args!!0))::Integer b = (read (args!!1))::Integer result = binom a b putStrLn (show result) binom :: Integer -> Integer -> Integer binom n k | k == 0 && n >= 0 = 1 | n < k && n >= 0 = 0 |(n-k) <= 5 = binom2 n k -- Abbruch (seq. Berechnung) | k <= n && k >= 0 = let b1 = binom (n-1) k b2 = binom (n-1) (k-1) in --- parallele Berechnung --- b1 `par` b2 `seq` (b2 + b1) ---------------------------- | otherwise = error "negative Parameter" ---------------------------------------- fac :: Integer -> Integer fac 0 = 1 fac n | n < 0 = error ("Fakultät " ++ (show n) ++ " nicht definiert.") | otherwise = n * fac (n-1) --simple binom2 n k | k == 0 && n >= 0 = 1 | n < k && n >= 0 = 0 | k <= n && k >= 0 = (fac n) `div` (fac (n-k) * (fac k)) | otherwise = error "negative Parameter"