5 Replies - 3272 Views - Last Post: 09 December 2011 - 10:47 AM

#1 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Assignment in Haskell ... How to star? :S

Posted 07 December 2011 - 07:59 AM

Hi all!
I have an assignment that’s make me crazy!

The story is like this.
Someone have 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")])


So, I got something like:

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")])]


Part of my assignment (the part where I’m stuck in) is: pick the restaurantlist and add, replace, change the order, edit the menus, the restaurants and the restaurant chain list.
I’m not asking to resolve my homework, because I really want to learn, but some help in one or two exercises, it would be great.

Question1: make a function that allows to edit a 3-tuple (a recipe) from a menu, given the restaurant name position, the menu position and the new recipe.
example: editRecipe 1 2 (2222,"SSSSSS","llllll")
output:
[("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")])]


Question2: make a function that allows to change the restaurants order.
Example – change "Food and friends" for "All in" :
changeorder 1 2
output:
[("All in",10,[(2.5,"Stracotto","Garlic"),(3.0,"Roast Beef","Butter"),(3.3,"Veal Chops","Pepper")]),("Food and friends",20,[(2.5,"Steak","lemon"),(3.5,"Vegetarian Meals","tomato"),(4.0,"Italian Beef","banana")]),("Orange",25,[(4.5,"Turkey","Mustard"),(5.1,"Chicken","egg"),(6.0,"Chicken Salad","fruit")])]


I really appreciate any possible help.

Is This A Good Question/Topic? 0
  • +

Replies To: Assignment in Haskell ... How to star? :S

#2 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Re: Assignment in Haskell ... How to star? :S

Posted 07 December 2011 - 12:17 PM

For Question1. I've tried the fowling approach:

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"



But this code need a list (newrecipe) as argument, but I need a 3-tuple. :/
ex: editRecipe 1 2 [(5.1,"xxxxxxxxx","ccccccccccc")]
and I need:
editRecipe 1 2 (5.1,"xxxxxxxxx","ccccccccccc")
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Assignment in Haskell ... How to star? :S

Posted 07 December 2011 - 02:47 PM

Okay, so your task is to take a list and modify the nth element of that list. You know how'd you perform the modification on a single element, so all you need is a way to perform some modification on a given element of a list (which is to say create a new list containing the elements of the original list with the nth element replaced by its modification).

You could either achieve this by using recursion and counting down the argument n until it reaches zero and then replace the element at that position. Or you could use take, !! and drop to create a list consisting of the first (n-1) elements of the list (take (n - 1)), followed by the nth element (!! n) modified, followed by all but the first n elements (drop n).

Note that randomly accessing a linked list like this is terribly inefficient and something like this should never be used in real code, but it can't be avoided with this assignment.
Was This Post Helpful? 1
  • +
  • -

#4 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Re: Assignment in Haskell ... How to star? :S

Posted 07 December 2011 - 03:31 PM

Vielen dank sepp2k.

Ich werde versuchen, die recursion zu verwenden. Dieses thema ist nicht meine stärke.
Ich hoffe, das ich mit ein wenig lernen etwas besser werde.
:)
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Assignment in Haskell ... How to star? :S

Posted 09 December 2011 - 09:27 AM

for future reference, it's preferred that you post in English. it helps us help more people :)
Was This Post Helpful? 0
  • +
  • -

#6 Paticia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-December 11

Re: Assignment in Haskell ... How to star? :S

Posted 09 December 2011 - 10:47 AM

View Postishkabible, on 09 December 2011 - 09:27 AM, said:

for future reference, it's preferred that you post in English. it helps us help more people :)

Sorry ... It was just to say "thanks" to sepp2k by his help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1