# a series of unit conversion calculations.. not sure how to do in Haskell

Submitted by metaperl on Fri, 09/09/2005 - 12:56pm.

-- 2 oranges make 8 ounces of juice

ounces 8 = Oranges 2

-- a 32.oz non-organic orange juice costs 4.55 at Jamba Juice

ounces 32 = Cost 4.55

-- at retail price, I get 72 organic oranges for 10.00

-- which means 7.2 oranges for 1.00

-- which means 15 cents per orange

-- which means 30 cents for 8 ounces

-- which means 1.20 for 32 ounces

-- which means 3.35 cheaper than Jamba Juice and organic produce

-- I can get much cheaper prices on oranges if I buy in bulk

- metaperl's blog
- Login to post comments

Here's some code :)

----

{-# OPTIONS_GHC -fglasgow-exts #-}

newtype Oranges = Oranges Double deriving (Show, Eq, Ord, Num)

newtype Juice = Juice Double deriving (Show, Eq, Ord, Num)

newtype Dollars = Dollars Double deriving (Show, Eq, Ord, Num)

squeeze :: Oranges -> Juice

squeeze (Oranges x) = Juice (8 * x/2)

buyOranges :: Dollars -> Oranges

buyOranges (Dollars x) = Oranges (72 * x/10)

buyJambaJuice :: Dollars -> Juice

buyJambaJuice (Dollars x) = Juice (32 * x/4.55)

----

In ghci:

*Main> buyJambaJuice 1

Juice 7.032967032967033

*Main> (squeeze . buyOranges) 1

Juice 28.8

So we can see that we're getting a good deal more juice for a dollar from buying and squeezing the juice ourselves. :)