2011-12-08 14 views
0

そして、このようになります。 誰かがレストランチェーンを持っています。各レストランでは、メニュー(価格、「レシピ名」、「特別な成分」)と「レストラン名」、テーブルの数と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")])] 

一部です:メニュー、レストラン、レストランチェーンを編集し、順序を変更、置き換え、restaurantlistを選択し、追加し、私のような何かを得ましたリスト。

質問:レストラン名の位置、メニューの位置、新しいレシピを指定して、メニューから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") 

しかし、私のコードで、私はリストを挿入する必要がありますが、引数としてタプルを聖霊降臨祭。問題は、リストではなく引数としてタプルだけを挿入できるのでしょうか?

+5

その引数は3タプルまたは値の他の種類がある場合、それは問題ではありません。それは前にこの問題をどこで解決したのと同じ方法で動作するはずです。 – sth

+2

コンパイルされていなくても失敗したコードを提供すれば、SOコミュニティはより良い対応をします。助言を与え、より簡単な方法を指摘することを許可する。 –

+0

この質問はあいまいです。あなたの機能が正確に何をしたいですか? – Prateek

答えて

1

質問はあいまいですが、おそらくこれが役立ちます。

実は、あなたがいないなど編集タプル、リスト、代わりにあなたが古いからなど新しいタプル、リストを構築する必要がありますすることができます。たとえば、位置n[Int]でいくつかのIntを挿入する:次に

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] 
関連する問題