My Weblog

Blog about programming and math

SPOJ Conversions

Conversions is easy problem and nothing fascinating except solving it in Haskell. Learn today printf function in Haskell .

import Control.Monad
import Text.Printf

readD :: String -> Double
readD = read

main = do 
  n <- fmap read getLine
  forM_ ( [1..n] :: [Int] ) $ \a -> do 
	(x:y:_) <- fmap words getLine
    	let ( l , m )  = case y of 
		          "kg" -> ( readD x * 2.2046 , "lb" ) 
		          "lb" -> ( readD x * 0.4536 , "kg" )
		          "l"  -> ( readD x * 0.2642 , "g"  )
		          "g"  -> ( readD x * 3.7854 , "l" )
	printf "%d %0.4f %s\n" a  l m
Advertisements

May 18, 2011 - Posted by | Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: