# How to write a function from a definition?

Page 1 of 1

## 1 Replies - 884 Views - Last Post: 01 August 2015 - 02:04 AM

### #1 garth11

Reputation: 0
• Posts: 1
• Joined: 31-July 15

# How to write a function from a definition?

Posted 31 July 2015 - 04:52 AM

Hi,

First off, I would like to make you all aware that I'm very new to Haskell, so to increase knowledge etc, I've been trying out questions and I'm pretty stuck on one. I think I'm nearly there but some more experienced advice would be appreciated. Here's the question:

A sporting team is represented by it's name and the amount of points they scored in their last games like so ("Newcastle",[3,3,3,0]). This data is modelled by the type definitions:

```type TName = String
type Points = [Int]
type Team = (TName,Points)
```

From this I have to define the following function that orders one team higher than another if their points sum is greater:

```sortPoints :: [Team] -> [Team]
```

This is what I've tried:

```sortPoints :: [Team] -> [Team]
sortPoints [_,()] -> []
sortPoints [_,(x:xs)] -> sum[x|x<-xs]
```

Once I get to here, I'm not too sure how to go about adding the conditions to check the points sum, any pointers would be greatly appreciated as I'm still coming to terms with a lot of Haskell features.

Is This A Good Question/Topic? 0

## Replies To: How to write a function from a definition?

### #2 ishkabible

• spelling expret

Reputation: 1739
• Posts: 5,895
• Joined: 03-August 09

## Re: How to write a function from a definition?

Posted 01 August 2015 - 02:04 AM

```sortPoints :: [Team] -> [Team]
sortPoints [_,()] -> []
sortPoints [_,(x:xs)] -> sum[x|x<-xs]
```

This code won't even compile for so many reasons. It just doesn't even make sense. I'm not sure I consider this a full faith effort. It looks as though you just threw some Haskell looking symbols together and hoped for the best. That is far from an effective way to program.

Let's enumerate all the issues here. First off, why are you only matching on lists of length two? Why are you ignoring the first elements? Why are you using list comprehensions there? You are literally just constructing the same list all over again by doing that. You match the second element of the list on unit but then match the same element on a list; moreover neither types are correct types as a team is a pair of things. In the first equation you return an empty list which is potentially valid but In the second equation however you return an what I guess should be an integer. You figured out that you need to sum some list of numbers up but I'm extremely confused by what you think you are summing up? It isn't the points of any team at the very least. Finally the '->' is only used in types and *not* in defining equations. Use the '=' symbol to define equations; like so:

```sortPoints :: [Team] -> [Team]
sortPoints [_,?] = []
sortPoints [_,?] = ?

```

I put question marks in place of all the things that didn't make any sense at all. Still the matching on lists of length 2 makes no logical sense along with the underscore. Really your code might as well look like this.

```sortPoints :: [Team] -> [Team]
sortPoints ? = [] --some kind of base case where the empty list gets returned
sortPoints ? = ? --something to do with summation

```

which doesn't really express an attempt to me.

Lets break down the problem. We want to sort something here. This is non-trivial and you should probably look up sorting algorithms. You don't seem to have given much thought on how to sort anything at all much less by some complex criteria. So start by figuring out how to sort anything at all, say, a list of integers. From there figure out how to sort by arbitrary criteria by changing how you do the comparison. Finally figure out how to compute the criteria here (namely a teams total score) and then sort the list of teams by that criteria.

This post has been edited by ishkabible: 01 August 2015 - 02:09 AM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }