Man or boy test: Difference between revisions

m
→‎{{header|Haskell}}: Pruned leading spaces, applied hindent, traded liftM2 for liftA2, added type signatures, qualified imports.
(added Ol)
m (→‎{{header|Haskell}}: Pruned leading spaces, applied hindent, traded liftM2 for liftA2, added type signatures, qualified imports.)
Line 1,771:
Haskell is a pure language, so the impure effects of updating ''k'' must be wrapped in the IO or ST [[Monads|monad]]:
 
<lang haskell> import Control.MonadApplicative (liftA2)
import Data.IORef (modifyIORef, newIORef, readIORef)
 
a
a k x1 x2 x3 x4 x5 = do r <- newIORef k
:: (Enum a, Num b, Num a, Ord a)
let b = do k <- pred !r
=> a -> IO b -> IO b -> IO b -> IO b -> IO b -> a kIO b x1 x2 x3 x4
a k x1 x2 x3 x4 x5 = do r <- newIORef k
if k <= 0 then liftM2 (+) x4 x5 else b
r <- newIORef k
where f !r = modifyIORef r f >> readIORef r
let b = do
let b = do k <- pred ! r
a k b x1 x2 x3 x4
if k <= 0
then liftA2 (+) x4 x5
else b
where
where f !r = modifyIORef r f >> readIORef r
 
main :: IO ()
main = a 10 #1 #(-1) #(-1) #1 #0 >>= print
main = a 10 # where1 # (-1) # (-1) f# =1 f# .0 return</lang>>= print
where
( # ) f = f . return</lang>
 
On an AMD Opteron 6282 SE using GHC 7.8.2 this program can compute ''k'' = 30 in 1064 s and 156.2 GiB.
9,655

edits