functional script

master
Yves Dubromelle 9 years ago
parent 74a832ff24
commit e6ab8cddc7

@ -1,34 +1,50 @@
#! /usr/bin/env runhaskell #! /usr/bin/env runhaskell
type Ingredient = (String, Double, Double) data Ingredient = Ingredient { ingredientName :: String
, unit_cost :: Double
}deriving (Show)
type Recette = ( String, [Ingredient], Double, Double) data UsedIngredient = UsedIngredient { ingredient :: Ingredient, quantityUsed :: Double }deriving (Show)
ingredientsLessive :: [Ingredient] data Recipe = Recipe { recipeName :: String
ingredientsLessive = ( "Lessive" , billOfMaterial :: [ UsedIngredient ]
, [ ("Eau", 0.003, 2.5) , finalQuantity :: Double
, ("Savon de Marseille", 3.36, 0.1) , useNumber :: Double
, ("Bicarbonate de soude", 3.81, 0.016) }deriving (Show)
, ("Vinaigre blanc", 0.36, 0.03)
]
, 2.5
, 30
)
quantiteLessive = 2.5 recipeCost :: Recipe -> Double
nombreUtilisations = 30 recipeCost (Recipe _ bom _ _) = sum . map ingredientCost $ bom
prixTotal ingredientsLessive = sum . map (\ (_, x, y) -> x * y) $ ingredientsLessive ingredientCost :: UsedIngredient -> Double
ingredientCost (UsedIngredient (Ingredient _ uCost) qtt) = uCost * qtt
prixLitre prixTotal quantiteLessive = prixTotal / quantiteLessive prixLitre prixTotal quantiteLessive = prixTotal / quantiteLessive
prixUtilisation prixTotal quantiteLessive = prixTotal / nombreUtilisations prixUtilisation prixTotal quantiteLessive = prixTotal / nombreUtilisations
eau = Ingredient "Eau" 0.003
savonMarseille = Ingredient "Savon de Marseille" 3.36
bicarbonate = Ingredient "Bicarbonate de soude" 3.81
vinaigreBlanc = Ingredient "Vinaigre blanc" 0.36
lessive :: Recipe
lessive = Recipe "Lessive"
[ UsedIngredient eau 2.5
, UsedIngredient savonMarseille 0.1
, UsedIngredient bicarbonate 0.016
, UsedIngredient vinaigreBlanc 0.03
]
2.5
30
quantiteLessive = 2.5
nombreUtilisations = 30
main = do main = do
putStrLn "Calcul du coût des recettes\n" putStrLn "Calcul du coût des recettes\n"
putStrLn "* Lessive :" putStrLn "* Lessive :"
print ingredientsLessive print lessive
putStrLn $ " * Prix Total = " ++ show prixTotal ++ "" putStrLn $ " * Prix Total = " ++ show (recipeCost lessive) ++ ""
putStrLn $ " * Prix au litre = " ++ show prixLitre ++ "" -- putStrLn $ " * Prix au litre = " ++ show prixLitre ++ "€"
putStrLn $ " * Prix par utilisation = " ++ show prixUtilisation ++ "" -- putStrLn $ " * Prix par utilisation = " ++ show prixUtilisation ++ "€"

Loading…
Cancel
Save