编辑给定位置的列表?

时间:2011-12-08 03:40:05

标签: list haskell

就是这样。 有人有一连串的餐馆。每个餐厅都是一个带有“餐馆名称”的3元组,多个餐桌和一个带有菜单的3元组列表(价格,“食谱名称”,“特殊成分”):

("restaurant name", number of tables, [(price, "recipe name" , "special ingredient")])

所以,我有类似的东西:

chainRestaurants = [("Food and friends",20,[(2.5,"Steak","lemon"),(3.5,"Vegetarian Meals","tomato"),(4.0,"Italian Beef","banana")]),("All in",10,[(2.5,"Stracotto","Garlic"),(3.0,"Roast Beef","Butter"),(3.3,"Veal Chops","Pepper")]),("Orange",25,[(4.5,"Turkey","Mustard"),(5.1,"Chicken","egg"),(6.0,"Chicken Salad","fruit")])]

我的部分工作(我被困住的部分)是:选择餐馆列表并添加,更换,更改订单,编辑菜单,餐馆和餐馆连锁列表。

因此,问题:根据餐馆名称位置,菜单位置和新配方,制作一个允许从菜单中编辑3元组(食谱)的功能。

示例:editRecipe 1 2(2222,“SSSSSS”,“llllll”)

输出:

[("Food and friends",20,[(2.5,"Steak","lemon"),(2222,"SSSSSS","llllll"),(4.0,"Italian Beef","banana")]),("All in",10,[(2.5,"Stracotto","Garlic"),(3.0,"Roast Beef","Butter"),(3.3,"Veal Chops","Pepper")]),("Orange",25,[(4.5,"Turkey","Mustard"),(5.1,"Chicken","egg"),(6.0,"Chicken Salad","fruit")])]

我已经解决了这个问题,但从来没有以3元组作为参数......我需要它! :/

我的代码是:

chainRestaurants = [("Food and friends",20,[(2.5,"Steak","lemon"),(3.5,"Vegetarian Meals","tomato"),(4.0,"Italian Beef","banana")]),("All in",10,[(2.5,"Stracotto","Garlic"),(3.0,"Roast Beef","Butter"),(3.3,"Veal Chops","Pepper")]),("Orange",25,[(4.5,"Turkey","Mustard"),(5.1,"Chicken","egg"),(6.0,"Chicken Salad","fruit")])]

menu1 = [(2.5,"Steak","lemon"),(3.5,"Vegetarian Meals","tomato"),(4.0,"Italian Beef","banana")]
restaurant1 = ("Food and friends",20,menu1)

menu2 = [(2.5,"Stracotto","Garlic"),(3.0,"Roast Beef","Butter"),(3.3,"Veal Chops","Pepper")]
restaurant2 = ("All in",10,menu2)

menu3 = [(4.5,"Turkey","Mustard"),(5.1,"Chicken","egg"),(6.0,"Chicken Salad","fruit")]
restaurant3 = ("Orange",25,menu3)

chainRestaurants1 = [restaurant1] ++ [restaurant2] ++ [restaurant3]


editRecipe restaurantposition menuposition newrecipe | menuposition == 0 = error "No such thing" 
                                                 | restaurantposition == 1 &&   menuposition > length menu1 = error "No such thing" 
                                                 | restaurantposition == 2 && menuposition > length menu2 = error "No such thing" 
                                                 | restaurantposition == 3 && menuposition > length menu3 = error "No such thing" 

editRecipe restaurantposition menuposition newrecipe = case restaurantposition of 1 -> [("Food and friends",20, take (menuposition-1) menu1 ++ newrecipe ++ drop (menuposition) menu1)] ++ [restaurant2] ++ [restaurant3]
                                                                                2 -> [restaurant1] ++ [("Food and friends",20, take (menuposition-1) menu2 ++ newrecipe ++ drop (menuposition) menu2)] ++ [restaurant3]
                                                                                3 -> [restaurant1] ++ [restaurant2] ++ [("Orange",25, take (menuposition-1) menu3 ++ newrecipe ++ drop (menuposition) menu3)]
                                                                                otherwise -> error "No such thing"

但是这段代码需要一个列表(newrecipe)作为参数,但我需要一个3元组。 :/

ex: editRecipe 1 2 [(5.1,"xxxxxxxxx","ccccccccccc")]

我需要:

editRecipe 1 2 (5.1,"xxxxxxxxx","ccccccccccc") 

但是使用我的代码,我必须插入一个以元组为目标的列表作为参数。问题是:是否可以只插入一个元组作为参数而不是列表?

1 个答案:

答案 0 :(得分:1)

问题很模糊,但也许这会有所帮助。

实际上,你不能编辑元组,列表等。相反,你应该从旧构建构建新元组,列表等。例如,您想在位置Int的{​​{1}}中插入一些[Int]

n

然后:

insert :: Int -> Int -> [Int] -> [Int]
insert n x xs = before ++ [x] ++ after
  where
    (before, after) = splitAt n xs