Permalink
Browse files

Two Strategies examples

  • Loading branch information...
simonmar committed Mar 6, 2013
1 parent dd18920 commit c263dca2405d25bdb16dcfd9c325da21162fbc2d
Showing with 61 additions and 0 deletions.
  1. +28 −0 strat.hs
  2. +33 −0 strat2.hs
View
@@ -0,0 +1,28 @@
+import Control.Parallel
+import Control.Parallel.Strategies (rpar, Strategy, using)
+import Control.Exception
+import Data.Time.Clock
+import Text.Printf
+import System.Environment
+
+-- <<fib
+fib :: Integer -> Integer
+fib 0 = 1
+fib 1 = 1
+fib n = fib (n-1) + fib (n-2)
+-- >>
+
+main = print pair
+ where
+ pair =
+-- <<pair
+ (fib 35, fib 36) `using` parPair
+-- >>
+
+-- <<parPair
+parPair :: Strategy (a,b)
+parPair (a,b) = do
+ a' <- rpar a
+ b' <- rpar b
+ return (a',b')
+-- >>
View
@@ -0,0 +1,33 @@
+import Control.Parallel
+import Control.Parallel.Strategies (rpar, Strategy, using)
+import Control.Exception
+import Data.Time.Clock
+import Text.Printf
+import System.Environment
+
+-- <<fib
+fib :: Integer -> Integer
+fib 0 = 1
+fib 1 = 1
+fib n = fib (n-1) + fib (n-2)
+-- >>
+
+main = print pair
+ where
+ pair =
+-- <<pair
+ (fib 35, fib 36) `using` evalPair rpar rpar
+-- >>
+
+-- <<evalPair
+evalPair :: Strategy a -> Strategy b -> Strategy (a,b)
+evalPair sa sb (a,b) = do
+ a' <- sa a
+ b' <- sb b
+ return (a',b')
+-- >>
+
+-- <<parPair
+parPair :: Strategy (a,b)
+parPair = evalPair rpar rpar
+-- >>

0 comments on commit c263dca

Please sign in to comment.