Shoot self in foot

Shawn P. Garbett listman@garbett.org
Tue, 24 Jun 2003 15:24:01 -0500


Based on the earlier posted joke on this list, I put together the following 
code. Could probably use a bit of simplification, but you get the picture.

import IO
import GHC.IOBase

-- True = nice foot, False = shot up by some lunatic
type Foot = Bool

-- Manage a request from the user, given request and current foot state
manage   :: Char -> Foot -> IO Foot
manage c foot 
  -- InterleaveIO Makes the request "lazy"
  | c == '1'  =  unsafeInterleaveIO $
                  do 
                    putStr "OUCH!!!! Shoot Self in Foot\n"
                    return False -- The foot is now shot
  -- The doctor examines the state of the foot variable
  | c == '2'  = do
                  putStr "Doctor examines foot\n"
                  if foot
                    then putStr "I have a good foot.\n"
                    else putStr "I have a shot-up foot.\n"
                  return foot
  -- Otherwise just keep processing
  | otherwise = do
                  return foot

-- Main processing loop
process   :: Foot -> IO ()
process f =  do
               c <- getChar         -- Get a character
               putChar '\n'         -- Return to keep the screen neat
               if c == '0'          -- Is it an exit?
                 then return ()     -- then leave
                 else do
                   f' <- manage c f -- otherwise manage request
                   process f'       -- continue to process

-- Little usage message
usage :: IO ()
usage =  do
           putStr "0. Exit Program\n"
           putStr "1. Pull Trigger\n"
           putStr "2. Call Doctor\n"

-- Main routine
main :: IO ()
main =  do
          usage         -- Print usage
          process True  -- Start processing with a good foot