Cogs and Levers A blog full of technical stuff

Scissors, Paper, Rock!

Haskell is a strange beast at times. I think you can see from the brute-force approach on this implementation of SPR that I was getting clearly frustrated with even some of the simplest of things.

I really could have used “Maybe” I really could have used “Read”

It’s a learning experience:

 
module Main where

   import System.IO
   import System.Random

   data Move = Scissors | Paper | Rock | Unknown deriving (Eq,Show)
   data Outcome = Winner | Draw | Loser | ND deriving (Show)

   str2Move :: String -> Move
   str2Move s = do
      case s of
         "s" -> Scissors
         "p" -> Paper
         "r" -> Rock
         _   -> Unknown

   getWinner :: Move -> Move
   getWinner m = do
      case m of
         Scissors -> Rock
         Rock     -> Paper
         Paper    -> Scissors
         Unknown  -> Unknown

   getOutcome :: Move -> Move -> Outcome
   getOutcome player cpu
      | player == Unknown || cpu == Unknown = ND
      | player == cpu = Draw
      | cpu == winner = Loser
      | otherwise = Winner
      where winner = getWinner player

   getCpuMove :: StdGen -> Move
   getCpuMove gen = do
      let (randNumber, newGen) = randomR(1, 3) gen :: (Int, StdGen)
      case randNumber of
         1 -> Rock
         2 -> Scissors
         3 -> Paper

   main = do
      gen <- getStdGen
      putStr "Enter your choice (s, p or r): "
      hFlush stdout
      line <- getLine

      let player = str2Move line
      let cpu = getCpuMove gen
      let outcome = getOutcome player cpu

      putStrLn $ "Player Chose: " ++ (show player)
      putStrLn $ "Cpu Chose   : " ++ (show cpu)
      putStrLn $ "Outcome     : " ++ (show outcome)

Some who have been nice enough to comment from time to time have suggested that I move forward with this implementation (inclusion of Lizard, Spock).

Anyway, I’ll keep struggling.

Tools for Webgeneers

This one came across the news wire today. I just wanted to make a note of it so that I don’t loose it.

There’s plenty in here that will allow you to take any site from 0 to hero.

http://ivanzuzak.info/2012/11/18/the-web-engineers-online-toolbox.html

Daemons in Python

Didn’t want to lose this bookmark, so thought I’d pop it here for later.

I would expect that you’ll see some posts about this topic shortly.

http://blip.tv/episode/5592905

Be nice and die well

It’s a morbid title, I agree but it is quite important. Unix daemons really need to listen to the messages (read as: signals) being sent to them and responding correctly. At the very least, if every daemon implemented the following it’d be a breath of fresh air:

/** Responds to signals of interest */                                                                    
void daemon_signalhandler(int sig) {                                      
                                                                            
   switch (sig) {                                                         
                                                                            
      case SIGHUP:                                                        
         /* this should just refresh configs and restart */                                      
         break;                                                           
                                                                            
      case SIGTERM:                                                       
         /* mark the server as no longer running */                                               
         break;                                                           
   }                                                                      
                                                                            
}                                                                         

This block of code by itself is pretty useless. The other half of the equation is attaching this function to the signals of interest as well as ignoring signals we’re not interested in (or that we’re not interested in those signals turning our daemon to toast).

/* attach the signal handlers now */                                  
signal(SIGCHLD, SIG_IGN);                                             
signal(SIGTSTP, SIG_IGN);                                             
signal(SIGTTOU, SIG_IGN);                                             
signal(SIGTTIN, SIG_IGN);                                             
signal(SIGHUP, daemon_signalhandler);                                 
signal(SIGTERM, daemon_signalhandler);                                

Well, that’s it really and remember - die well.

Movie Time: Unix!

Breakout the pop-corn, it’s movie time.

http://techchannel.att.com/play-video.cfm/2012/2/22/AT&T-Archives-The-UNIX-System