Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Shootout/Chameneos
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Current version == This takes 20% (or so) less time than the two other versions I downloaded and tested, probably because it does only 3 putMVar and 3 takeMVar per meeting. It also shifts the responsibility for printing the result onto the last creature to fade; otherwise the printing by the manager thread could be construed as asymmetry. I can't bear to ditch the field labels and secondary indentation for the sake of a smaller gzipped source file. But if you want to, please feel free. *sniff* Submitted, 17 Nov 2006 Accepted and ranked #1, 18 Nov 2006 <haskell> {- The Computer Language Shootout http://shootout.alioth.debian.org/ written by Tom Pledger, 13 Nov 2006 http://www.haskell.org/haskellwiki/Great_language_shootout -} import Control.Concurrent import System data Colour = Red | Yellow | Blue | Faded complement Red Red = Red complement Red Yellow = Blue complement Red Blue = Yellow complement Yellow Red = Blue complement Yellow Yellow = Yellow complement Yellow Blue = Red complement Blue Red = Yellow complement Blue Yellow = Red complement Blue Blue = Blue complement _ _ = Faded data MeetingPlace = MP{ quota :: !Int, waiter :: !(Maybe Colour), done :: ![Int] } main = do args <- getArgs mpv <- newMVar MP{ quota = case args of [] -> 1000000; s:_ -> read s, waiter = Nothing, done = [] } wakerv <- newEmptyMVar let arrive colour tally = do mp <- takeMVar mpv case mp of MP{ quota = 0, done = d } -- Faded now, but nobody wants to know | length d == length subCols -> print (tally + sum d) | otherwise -> putMVar mpv mp{ done = tally:d } MP{ waiter = Nothing } -> do putMVar mpv mp{ waiter = Just colour } colour' <- takeMVar wakerv arrive colour' $! tally + 1 MP{ quota = q, waiter = Just colour0 } -> do let colour' = complement colour0 colour putMVar wakerv $! colour' putMVar mpv mp{ quota = q - 1, waiter = Nothing } arrive colour' $! tally + 1 subCols = [Blue, Red, Yellow] sequence_ [forkIO (arrive c 0) | c <- subCols] arrive Blue 0 sequence_ [yield | c <- subCols] </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width