List 在给定位置编辑列表?
事情是这样的。 有人开了连锁餐厅。每个餐厅都是一个三元组,带有“餐厅名称”、多张桌子和一个三元组列表,其中包含菜单(价格、“食谱名称”、“特殊配料”): 所以,我得到了如下结果:List 在给定位置编辑列表?,list,haskell,List,Haskell,事情是这样的。 有人开了连锁餐厅。每个餐厅都是一个三元组,带有“餐厅名称”、多张桌子和一个三元组列表,其中包含菜单(价格、“食谱名称”、“特殊配料”): 所以,我得到了如下结果: chainRestaurants = [("Food and friends",20,[(2.5,"Steak","lemon"),(3.5,"Vegetarian Meals","tomato"),(4.0,"Italian Beef","banana")]),("All in",10,[(2.5,"Stracott
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元组(菜谱)
示例:编辑配方12(2222,“SSSS”、“LLLL”)
输出:
[("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")])]
我已经解决了这个问题,但从来没有用三元组作为参数。。。我需要它/
这是我的密码:
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")
但在我的代码中,我必须插入一个带有元组的列表作为参数。问题是:是否可以只插入一个元组作为参数而不插入一个列表?这个问题很模糊,但这可能会有所帮助 实际上,您不能编辑元组、列表等。相反,您应该从旧的元组、列表等构建新的元组、列表等。例如,您想在
[Int]
位置n
插入一些Int
:
insert :: Int -> Int -> [Int] -> [Int]
insert n x xs = before ++ [x] ++ after
where
(before, after) = splitAt n xs
然后:
不管那个参数是3元组还是其他类型的值。。。它的工作方式应该与您以前解决此问题的方式相同。因此,如果您提供失败的代码,即使它没有编译,社区的响应也会更好。允许我们给出建议并指出更简单的方法。这个问题很模糊。你到底希望你的函数做什么。已添加我的代码。:)如果您有tuple
t
,您可以通过[t]
或返回t
从中创建列表。
insert :: Int -> Int -> [Int] -> [Int]
insert n x xs = before ++ [x] ++ after
where
(before, after) = splitAt n xs
> insert 5 100 [1..10]
[1,2,3,4,5,100,6,7,8,9,10]