6 Replies - 1361 Views - Last Post: 02 February 2012 - 07:19 AM

Topic Sponsor:

#1 pistolizedpete  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 02-December 08

Writing Haskell Functions

Posted 16 January 2012 - 07:40 AM

Hello there I have a test coming up this week so studying sample papers.
This is a question on one of the papers that I cant figure out:

Write a Haskell function that calculates the mean of a list of odd numbers that
are not divisible by 3 or 5 and whose sum is less than 2000.


It's not so much the maths of the question.I know how to use mod function to work out what is and isnt divisible
by 3 etc.

It's more the structure of the code. ie Dealing with empty lists and how to actually write
the function if you know what I mean? If you could give a full working answer that would be
reat but, any other advice you have is greatly appreciated.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing Haskell Functions

#2 ishkabible  Icon User is offline

  • spelling expert
  • member icon



Reputation: 1139
  • View blog
  • Posts: 4,775
  • Joined: 03-August 09

Re: Writing Haskell Functions

Posted 16 January 2012 - 11:14 AM

break it down; what parts can we identify as translating to Haskell concepts?

Quote

odd numbers that are not divisible by 3 or 5


sounds like a predicate to me. you can either do some fancy composition and currying to create a function(my preference), you can create a lambda function, you can define it in a let or where clause, or you can make it a supercombinator(global function).

Quote

list of <items that satisfy the predicate> whose sum is less than 2000


the prelude function "filter" can filter out all the items that do not meet the predicate.

Quote

the mean of a <list we already described>


mean is defined as the sum(there is prelude function that dose that) divided by the length(also a prelude function for that)

hope this helps :)

edit:
the description of that functions is a bit ambiguous. Is it asking you to sum a list that just happens to be composed of items that satisfy the predicate? or are you supposed to filter out the items? or are you supposed to return a Maybe value depending on weather the list is composed of items that satisfy the predicate? my initial interpretation was that it was asking you to filter out the items and return a maybe value depending on weather or not it the sum was less than 2000 but I'm not sure that's what it is asking.

This post has been edited by ishkabible: 16 January 2012 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 pistolizedpete  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 02-December 08

Re: Writing Haskell Functions

Posted 18 January 2012 - 07:30 AM

It's my understanding that the list entered can be a list of any amount of integers odd or even.
I think we are required to filter all of the odd numbers that are not divisible by 3 or 5 whos
sum is less than 2000 and find the mean of the new list to clear things up.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#4 pistolizedpete  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 02-December 08

Re: Writing Haskell Functions

Posted 18 January 2012 - 07:51 AM

Actually it is simpler than that apparently...The question is jut asking for the mean of THE list of odd numbers not divisible by 3 or 5 from 1 to 2000. Should be a one line answer.

Whoops, I meant whos sum is less than 2000 not "from 1 to 2000".
Was This Post Helpful? 0
  • +
  • -

#5 pistolizedpete  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 02-December 08

Re: Writing Haskell Functions

Posted 18 January 2012 - 08:15 AM

Program crashes at 997 when this line is entered:

answer = [x | x <- [1,3..], mod x 3 /= 0 && mod x 5 /= 0, (x + x) < 2000]


Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 620
  • View blog
  • Posts: 1,037
  • Joined: 21-June 11

Re: Writing Haskell Functions

Posted 18 January 2012 - 09:12 AM

First of all you can't use conditions in list comprehensions to determine when the list ends. Haskell doesn't know that (x + x) < 2000 will never become true again once it's false for the first time. So once it becomes false, Haskell just keeps searching for the next number such that it's true again until the end of time. If you want to take all items of a list until x+x exceeds 2000, you should use takewhile instead:

answer = takeWhile (\x -> x + x < 2000) [x | x <- [1,3..], mod x 3 /= 0 && mod x 5 /= 0]


However, it should be pointed out that checking whether x+x is less than 2000 is not the same as checking whether the sum of the list so far is less than 2000. That will require more complicated logic.
Was This Post Helpful? 2
  • +
  • -

#7 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Writing Haskell Functions

Posted 02 February 2012 - 07:19 AM

It looks like an exercise on guards.

The solutions suggested above, don't take the mean of a given list, they take the mean of all whole numbers >= 1, not evenly divisable by 5 or 3 and who's sum does not exceed 2000. I don't see that in the assignment.

mean [] = 0
mean list |  sum list < 2000 
          && (filter (\x -> (mod x 3) == 0) list) == []
          && (filter (\x -> (mod x 5) == 0) list) == []
              = (fromIntegral (sum list)) / (fromIntegral (length list))



This will work, still plenty to improve upon. Filter will go all the way to the end while it doesn't have to. The sum can also stop early provided there are no negative numbers in the list and checking all 3 conditions in a single pass will be more efficient, but I've gone for clarity here.

This post has been edited by Karel-Lodewijk: 03 February 2012 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1