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

#1 garth11  Icon User is offline

  • New D.I.C Head

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

Thanks for your time.

Is This A Good Question/Topic? 0
  • +

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

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1739
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1