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

Page 1 of 1

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

### #1 Paticia

Reputation: 0
• 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

Reputation: 0
• 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")])]

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"

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

### #3 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,574
• 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.

### #4 Paticia

Reputation: 0
• 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.

### #5 ishkabible

• spelling expret

Reputation: 1716
• Posts: 5,868
• 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

### #6 Paticia

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

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

Posted 09 December 2011 - 10:47 AM

ishkabible, 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.