Skip to content

Commit

Permalink
Merge pull request joewalnes#114 from FranklinChen/haskell
Browse files Browse the repository at this point in the history
More work on Haskell examples
  • Loading branch information
asergeyev committed Feb 17, 2015
2 parents 9a9dc3a + 51fa56e commit 7e2d01c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/haskell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ $ websocketd --port=8080 --devconsole --passenv PATH ./count.hs
```

The passing of `PATH` was required for me because a typical Haskell installation of `runhaskell` does not go into `/usr/bin` but more like `/usr/local/bin`.

### Greeter

The greeter server waits for a line of text to be sent, then sends back a greeting in response, and continues to wait for more lines to come.

```
$ websocketd --port=8080 --devconsole --passenv PATH ./greeter.hs
```
2 changes: 2 additions & 0 deletions examples/haskell/count.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import Control.Monad (forM_)
import Control.Concurrent (threadDelay)
import System.IO (hFlush, stdout)

-- | Count from 1 to 10 with a sleep
main :: IO ()
main = forM_ [1 :: Int .. 10] $ \count -> do
print count
hFlush stdout
threadDelay 500000
14 changes: 14 additions & 0 deletions examples/haskell/greeter.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env runhaskell

import Control.Monad (unless)
import System.IO (hFlush, stdout, stdin, hIsEOF)

-- | For each line FOO received on STDIN, respond with "Hello FOO!".
main :: IO ()
main = do
eof <- hIsEOF stdin
unless eof $ do
line <- getLine
putStrLn $ "Hello " ++ line ++ "!"
hFlush stdout
main

0 comments on commit 7e2d01c

Please sign in to comment.