Alright so I have two types:
data Ingredient = Ingredient { potato :: String, amount :: Int, cookingTime :: Int }
data Soup = Soup { availableTime :: Int, recipe :: [Ingredient], shoppingList :: [Ingredient], canBeDone :: Bool }
now if I want to compare the "available Time" and the "cookingTime" of an Ingredient (basically if I have enough time to cook that ingredient if I only have "availableTime" to cook the whole soup). And if I can cook the Ingredient in my soup, the Ingredient gets shifted into the shoppingList list. How do I go about that? Here's what I came up with:
doIHaveTime :: Soup -> Soup
doIHaveTime Soup{availableTime = a, recipe = [Ingredient{cookingTime = b}] } = if a >= b then Soup{ shoppingList = b:xs } else show "Can't be done."
does that way of thinking make sense? These are the errors I get:
soupExample.hs:6:128: error: • Couldn't match type ‘[Char]’ with ‘Soup’ Expected type: Soup Actual type: String • In the expression: show "Can't be done." In the expression: if a >= b then Soup {shoppingList = b : xs} else show "Can't be done." In an equation for ‘doIHaveTime’: doIHaveTime (Soup {availableTime = a, recipe = [Ingredient {cookingTime = b}]}) = if a >= b then Soup {shoppingList = b : xs} else show "Can't be done." Failed, modules loaded: none.
recipe = [Ingredient{cookingTime = b}]will only handle recipes with one ingredient, and crash on the other cases. You can userecipe = isto bind the whole list, or do two cases,recipe = []andrecipe = Ingredient{cookingTime = b} : is. - chiSoup -> Souplooks wrong, since you are not returning a soup if there's not enough time. Maybe you wantSoup -> Maybe Soup? - chishoppingListshould probably not be part ofSoup; that should be a list ofIngredients independent of whateverSouprequires the ingredients. - chepner