From e6ab8cddc799a2d20e43d6b9be45e65be48703d8 Mon Sep 17 00:00:00 2001 From: Yves Dubromelle Date: Sat, 21 May 2016 02:01:12 +0200 Subject: [PATCH] functional script --- cout-recette.hs | 54 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/cout-recette.hs b/cout-recette.hs index 9895ef5..9179e6d 100755 --- a/cout-recette.hs +++ b/cout-recette.hs @@ -1,34 +1,50 @@ #! /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] -ingredientsLessive = ( "Lessive" - , [ ("Eau", 0.003, 2.5) - , ("Savon de Marseille", 3.36, 0.1) - , ("Bicarbonate de soude", 3.81, 0.016) - , ("Vinaigre blanc", 0.36, 0.03) - ] - , 2.5 - , 30 - ) +data Recipe = Recipe { recipeName :: String + , billOfMaterial :: [ UsedIngredient ] + , finalQuantity :: Double + , useNumber :: Double + }deriving (Show) -quantiteLessive = 2.5 -nombreUtilisations = 30 +recipeCost :: Recipe -> Double +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 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 putStrLn "Calcul du coût des recettes\n" putStrLn "* Lessive :" - print ingredientsLessive - putStrLn $ " * Prix Total = " ++ show prixTotal ++ "€" - putStrLn $ " * Prix au litre = " ++ show prixLitre ++ "€" - putStrLn $ " * Prix par utilisation = " ++ show prixUtilisation ++ "€" + print lessive + putStrLn $ " * Prix Total = " ++ show (recipeCost lessive) ++ "€" +-- putStrLn $ " * Prix au litre = " ++ show prixLitre ++ "€" +-- putStrLn $ " * Prix par utilisation = " ++ show prixUtilisation ++ "€"