patternMinor
Mixed Drink Calculator in Haskell
Viewed 0 times
drinkcalculatormixedhaskell
Problem
This is a simple mixed drink calculator written in Haskell.
There are two input files. The
The
The calculator parses the drinks file and the ingredients file, determines which mixed drinks can be created with the available ingredients, and pretty-prints the results to screen:
It's a simple program, but I want to improve my code style and make my Haskell more idiomatic.
`import Text.ParserCombinators.Parsec
import Data.Char
import Data.List
type Drink = (DrinkName, [Ingredient])
type DrinkName = String
type Ingredient = String
-- parsing the file of drink recipes
drinksFile ∷ GenParser Char st [Drink]
drinksFile = endBy drink (char '\n')
-- parsing each drink recipe
drink ∷ GenParser Char st Drink
drink = do first ← drinkName
next ← recipe
return (first, next)
-- parsing the drink name
drinkName ∷ GenParser Char st DrinkName
drinkName = do name ← many (noneOf ":")
sep ← many (char ':')
return name
-- parsing the recipe into a list of ingredients
There are two input files. The
drinks file contains a list of simply formatted recipes for mixed drinks:screwdriver:vodka,orange juice
white russian:vodka,coffee liqueur,cream
greyhound:gin,grapefruit juice
...The
ingredients file contains a line-separated list of presently available ingredients:vodka
gin
rum
orange juice
cranberry juice
grapefruit juice
...The calculator parses the drinks file and the ingredients file, determines which mixed drinks can be created with the available ingredients, and pretty-prints the results to screen:
Screwdriver: Vodka, Orange juice
Greyhound: Gin, Grapefruit juice
...
It's a simple program, but I want to improve my code style and make my Haskell more idiomatic.
`import Text.ParserCombinators.Parsec
import Data.Char
import Data.List
type Drink = (DrinkName, [Ingredient])
type DrinkName = String
type Ingredient = String
-- parsing the file of drink recipes
drinksFile ∷ GenParser Char st [Drink]
drinksFile = endBy drink (char '\n')
-- parsing each drink recipe
drink ∷ GenParser Char st Drink
drink = do first ← drinkName
next ← recipe
return (first, next)
-- parsing the drink name
drinkName ∷ GenParser Char st DrinkName
drinkName = do name ← many (noneOf ":")
sep ← many (char ':')
return name
-- parsing the recipe into a list of ingredients
Solution
Good News:
Architecturally, your code looks very well designed. I can't think of any language features that would significantly improve your code's architecture. That leaves only function semantics and readability.
Advise:
This advise isn't specific to idiomatic Haskell but rather general code readability.
Your current main method does a little too much. the contents of main should have a high level of abstraction. Consider abstracting the work done at the end of your main function:
Here we have abstracted the process of printing the possible drinks into two sub methods. This provides greater readability and also allows us to change the return type of
By changing the argument order of a few functions, we can move the functions towards pointfree form:
Given the succinctness of your methods, moving towards pointfree form is quite readable and more idiomatic.
If we import the `
You can make the following equivalence substitutions:
Giving you:
All together we have:
Architecturally, your code looks very well designed. I can't think of any language features that would significantly improve your code's architecture. That leaves only function semantics and readability.
Advise:
This advise isn't specific to idiomatic Haskell but rather general code readability.
Your current main method does a little too much. the contents of main should have a high level of abstraction. Consider abstracting the work done at the end of your main function:
main = do drinks "Error parsing drinks file."
Right parsedDrinks -> showPossibleDrinks parsedDrinks (lines ingredients)
showPossibleDrinks :: [Drink] -> [Ingredient] -> String
showPossibleDrinks drinks ingredients = showDrinks $ filterByIngredients drinks ingredients
showDrinks :: [Drink] -> String
showDrinks = unlines . map printDrinkHere we have abstracted the process of printing the possible drinks into two sub methods. This provides greater readability and also allows us to change the return type of
case from IO to String. Hence the putStrLn outside the case statement.By changing the argument order of a few functions, we can move the functions towards pointfree form:
canMake :: [Ingredient] -> Drink -> Bool
canMake ingredients = all (flip elem ingredients) . snd
filterByIngredients :: [Ingredient] -> [Drink] -> [Drink]
filterByIngredients ingredients = filter (canMake ingredients)
main = do drinks "Error parsing drinks file."
Right parsedDrinks -> showPossibleDrinks (lines ingredients) parsedDrinks
showPossibleDrinks :: [Ingredient] -> [Drink] -> String
showPossibleDrinks = showDrinks . (filterByIngredients ingredients)Given the succinctness of your methods, moving towards pointfree form is quite readable and more idiomatic.
If we import the `
operator from Control.Applicative after transposing the function's arguments we can make filterByIngredients` completely pointfree:filterByIngredients :: [Ingredient] -> [Drink] -> [Drink]
filterByIngredients = filter canMakeYou can make the following equivalence substitutions:
(flip elem ingredients) ==> (`elem` ingredients)
intercalate " " ==> unwordsGiving you:
canMake :: [Ingredient] -> Drink -> Bool
canMake ingredients = all (`elem` ingredients) . snd
printDrink :: Drink -> String
printDrink (drinkName, recipe) = unwords (map capitalize (words drinkName)) ++
": " ++
intercalate ", " (map capitalize recipe)
where capitalize (x : xs) = toUpper x : xsAll together we have:
import Control.Applicative (())
import Data.Char
import Data.List
import Text.ParserCombinators.Parsec
type Drink = (DrinkName, [Ingredient])
type DrinkName = String
type Ingredient = String
-- parsing the file of drink recipes
drinksFile :: GenParser Char st [Drink]
drinksFile = endBy drink (char '\n')
-- parsing each drink recipe
drink :: GenParser Char st Drink
drink = do first Drink -> Bool
canMake ingredients = all (`elem` ingredients) . snd
filterbyIngredients :: [Ingredient] -> [Drink] -> [Drink]
filterbyIngredients = filter canMake
-- pretty prints a drink with its recipe
printDrink :: Drink -> String
printDrink (drinkName, recipe) = unwords (map capitalize (words drinkName)) ++
": " ++
intercalate ", " (map capitalize recipe)
where capitalize (x : xs) = toUpper x : xs
showPossibleDrinks :: [Ingredient] -> [Drink] -> String
showPossibleDrinks ingredients = showDrinks . filterbyIngredients ingredients
showDrinks :: [Drink] -> String
showDrinks = unlines . map printDrink
main = do drinks "Error parsing drinks file."
Right parsedDrinks -> showPossibleDrinks (lines ingredients) parsedDrinksCode Snippets
main = do drinks <- readFile "drinks"
ingredients <- readFile "ingredients"
putStrLn $
case parse drinksFile "drinksFile" drinks of
Left error -> "Error parsing drinks file."
Right parsedDrinks -> showPossibleDrinks parsedDrinks (lines ingredients)
showPossibleDrinks :: [Drink] -> [Ingredient] -> String
showPossibleDrinks drinks ingredients = showDrinks $ filterByIngredients drinks ingredients
showDrinks :: [Drink] -> String
showDrinks = unlines . map printDrinkcanMake :: [Ingredient] -> Drink -> Bool
canMake ingredients = all (flip elem ingredients) . snd
filterByIngredients :: [Ingredient] -> [Drink] -> [Drink]
filterByIngredients ingredients = filter (canMake ingredients)
main = do drinks <- readFile "drinks"
ingredients <- readFile "ingredients"
putStrLn $
case parse drinksFile "drinksFile" drinks of
Left error -> "Error parsing drinks file."
Right parsedDrinks -> showPossibleDrinks (lines ingredients) parsedDrinks
showPossibleDrinks :: [Ingredient] -> [Drink] -> String
showPossibleDrinks = showDrinks . (filterByIngredients ingredients)filterByIngredients :: [Ingredient] -> [Drink] -> [Drink]
filterByIngredients = filter <$> canMake(flip elem ingredients) ==> (`elem` ingredients)
intercalate " " ==> unwordscanMake :: [Ingredient] -> Drink -> Bool
canMake ingredients = all (`elem` ingredients) . snd
printDrink :: Drink -> String
printDrink (drinkName, recipe) = unwords (map capitalize (words drinkName)) ++
": " ++
intercalate ", " (map capitalize recipe)
where capitalize (x : xs) = toUpper x : xsContext
StackExchange Code Review Q#31288, answer score: 2
Revisions (0)
No revisions yet.