Finally I’ve finish my assignment. All my functions are working! \o/
Now I’m trying to improve my code (btw, I’m sure that my code sucks… but I’m still learning
And I really need/want to have a good grade on this assignment. So, any little help would be appreciated.
So, the story, once again, is like this: there's a chain of restaurants. Each restaurant is a 3-tuple with a “restaurant name”, an number of tables and a 3-tuple list with the menu (price, "recipe name" , "special ingredient") :
("restaurant name", number of tables, [(price, "recipe name" , "special ingredient")])
The fowling list is given:
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")])]
What I have to do is to edit the “restaurant name” and the "number of tables"
I was able to make (whit the help of dreamincode community _o_ ) 3 different codes for this question:
CODE 1.
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")])]
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)
chainRestaurants1 = [restaurant1] ++ [restaurant2]
editinfo3 1 newRestaurant_name newtableinfo = [(newRestaurant_name,newtableinfo,menu1)] ++ [restaurant2]
editinfo3 2 newRestaurant_name newtableinfo = [restaurant1] ++ [(newRestaurant_name,newtableinfo,menu2)]
editinfo3 _ newRestaurant_name newtableinfo = error "No such thing"
CODE 2.
chainRest1 [(_,_,[(c,d,e),(f,g,h),(i,j,k)]),(l,m,[(n,o,p),(q,r,s),(t,u,v)])] newRestaurant_name newtableinfo = [(newRestaurant_name,newtableinfo,[(c,d,e),(f,g,h),(i,j,k)]),(l,m,[(n,o,p),(q,r,s),(t,u,v)])] chainRest2 [(a,b,[(c,d,e),(f,g,h),(i,j,k)]),(_,_,[(n,o,p),(q,r,s),(t,u,v)])] newRestaurant_name newtableinfo = [(a,b,[(c,d,e),(f,g,h),(i,j,k)]),(newRestaurant_name,newtableinfo,[(n,o,p),(q,r,s),(t,u,v)])] editinfo2 1 newRestaurant_name newtableinfo = chainRest1 chainRestaurants newRestaurant_name newtableinfo editinfo2 2 newRestaurant_name newtableinfo = chainRest2 chainRestaurants newRestaurant_name newtableinfo editinfo2 _ newRestaurant_name newtableinfo = error "No such thing"
CODE 3.
editinfo1 1 newRestaurant_name newtableinfo = aux newRestaurant_name newtableinfo chainRestaurants
where aux a b ((_,_,c):dds) = (a,b,c):dds
editinfo1 2 newRestaurant_name newtableinfo = aux newRestaurant_name newtableinfo chainRestaurants
where aux x y ((a,b,c):(/>_,_,z):ds) = (a,b,c):(x,y,z):ds
editinfo1 _ newRestaurant_name newtableinfo = error "No such thing"
So, now I have 2 questions:
1. Which of those 3 answers have a "better" code?
2. Imagine that, instead of 2 restaurants, I have a chain of 100 restaurants. For any of my answers, it will give me a bunch of code lines, which make me thinking: there's should be another way to solve this, to prevent 100 or 200 lines of code for a case of 200 restaurants. Any ideas for a... more "pragmatic" way to solve this problem?
Ty all!!

New Topic/Question
Reply


MultiQuote








|