Guest User

mix_mix

a guest
Feb 9th, 2012
1,292
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {----------------------------------------------------------------------------
  2.  
  3. Компилировать:
  4.     ghc -O -threaded fact.hs
  5.  
  6. Использовать:
  7.     $ ./fact server 12345 &
  8.     Listening on 12345
  9.    
  10.     $ ./fact client ::1 12345
  11.     345345435435
  12.     Answer := [3,5,7,11,13,13,59,157,191]
  13.     -5.3
  14.     Answer := NaN
  15.     43534524542534543534
  16.     Answer := [2,485777,1495631,29960041]
  17.  
  18. ----------------------------------------------------------------------------}
  19.  
  20. import Network (listenOn, withSocketsDo, accept, connectTo, PortID(..), Socket)
  21. import System (getArgs)
  22. import System.IO (hSetBuffering, hGetLine, hPutStrLn, BufferMode(..), Handle)
  23. import Control.Concurrent (forkIO)
  24.  
  25. main :: IO ()
  26. main = do
  27.     args <- getArgs
  28.     case args of
  29.         ["server", port] -> server port
  30.         ["client", host, port] -> client host port
  31.         otherwise -> error "Usage: fact server [port] | fact client [host] [port]"
  32.  
  33. getPort :: String -> PortID
  34. getPort port = PortNumber $ fromIntegral (read port :: Int)  
  35.  
  36. -- client part
  37.  
  38. client :: String -> String -> IO ()
  39. client host port = withSocketsDo $ do
  40.     handle <- connectTo host $ getPort port
  41.     hSetBuffering handle NoBuffering
  42.     repl handle
  43.    
  44. repl :: Handle -> IO ()
  45. repl handle = do
  46.     getLine >>= hPutStrLn handle
  47.     putStr "Answer := "
  48.     hGetLine handle >>= putStrLn
  49.     repl handle
  50.    
  51. -- server part
  52.  
  53. server :: String -> IO ()
  54. server port = withSocketsDo $ do
  55.     sock <- listenOn $ getPort port
  56.     putStrLn $ "Listening on " ++ port
  57.     sockHandler sock
  58.  
  59. sockHandler :: Socket -> IO ()
  60. sockHandler sock = do
  61.     (handle, _, _) <- accept sock
  62.     hSetBuffering handle NoBuffering
  63.     forkIO $ processRequest handle
  64.     sockHandler sock
  65.  
  66. processRequest :: Handle -> IO ()
  67. processRequest handle = do
  68.     line <- hGetLine handle
  69.     hPutStrLn handle $ prettyPrint $ reads line
  70.     processRequest handle
  71.  
  72. prettyPrint :: [(Integer, String)] -> String
  73. prettyPrint [(number, _)] = show $ primeFactors number
  74. prettyPrint []            = "NaN"
  75.  
  76. primes = 2 : sieve [3, 5..] where
  77.     sieve (p:xs) = p : sieve [x | x <- xs, rem x p /= 0]
  78.  
  79. primeFactors :: Integer -> [Integer]
  80. primeFactors n = factor primes n where
  81.     factor ps@(p:pt) n | p*p > n      = [n]
  82.                        | rem n p == 0 = p : factor ps (quot n p)
  83.                        | otherwise    = factor pt n
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×