ZzzZzz
04-25-2010, 03:19 AM
Hello there, I'm fairly new to this whole haskell business and have been stuck on a few questions on my assignment that I've tweaked around with for a little while with no success.
So, I come here in hope that some of you more advanced programmers might be able to lend a hand! So here goes:
Q1.) Define a function 'largest', which returns the maximum element of a non-empty list of integers. You should not use the Prelude function 'maximum' in your answer and you should use recursion.
My attempts:
largest :: [Int] -> Int
largest [] = []
largest (head : tail) = if (head > tail) then head
else (largest tail)
Now im kinda unsure of how to use if..then..else statements within a list so my thought process was that if the value in head is greater than the rest of the values in the tail, then output head else run the function 'largest' on the tail
Q2.)Show the list of all positive integer multiples of 23 less than 1000 that end with the digit 7
My attempts:
--multiples :: [Int]
--multiples = [x | x <- [23], <empty> , x < 1000, x == __7]
Here I'm quite lost as to what to do, I'm thinking we need to have a function in there which adds 23 everytime the value doesn't end with a 7 or less than 1000 but I'm not sure how to implement a calculation function within a list comprehension. (The <empty> part was me taking out something that didn't make sense). Also I have no idea how to define a guard to check values that end with 7.
The -- notation is just so that my entire program will run. All in all I don't think this is right at all.
Q3.) Give a definition of 'intList' which produces the elements in increasing order with the following conditions:
1 is a member of 'intList'
If n is a member of intList, then so are 2*n+1 and 3*n+1
There are the only elements of intList
It's pretty much asking to print an infinite list beginning with 1, then adding on the numbers e.g. [1, 3, 4, 7....]
Now my attempts for this is pitiful, I ended up deleting everything except
intList :: [Int]
intList = [ x | x <- [1..]
as a result of nerdraging :o.
I know this was a long post so sorry for the inconvenience but any help would be much appreciated!
So, I come here in hope that some of you more advanced programmers might be able to lend a hand! So here goes:
Q1.) Define a function 'largest', which returns the maximum element of a non-empty list of integers. You should not use the Prelude function 'maximum' in your answer and you should use recursion.
My attempts:
largest :: [Int] -> Int
largest [] = []
largest (head : tail) = if (head > tail) then head
else (largest tail)
Now im kinda unsure of how to use if..then..else statements within a list so my thought process was that if the value in head is greater than the rest of the values in the tail, then output head else run the function 'largest' on the tail
Q2.)Show the list of all positive integer multiples of 23 less than 1000 that end with the digit 7
My attempts:
--multiples :: [Int]
--multiples = [x | x <- [23], <empty> , x < 1000, x == __7]
Here I'm quite lost as to what to do, I'm thinking we need to have a function in there which adds 23 everytime the value doesn't end with a 7 or less than 1000 but I'm not sure how to implement a calculation function within a list comprehension. (The <empty> part was me taking out something that didn't make sense). Also I have no idea how to define a guard to check values that end with 7.
The -- notation is just so that my entire program will run. All in all I don't think this is right at all.
Q3.) Give a definition of 'intList' which produces the elements in increasing order with the following conditions:
1 is a member of 'intList'
If n is a member of intList, then so are 2*n+1 and 3*n+1
There are the only elements of intList
It's pretty much asking to print an infinite list beginning with 1, then adding on the numbers e.g. [1, 3, 4, 7....]
Now my attempts for this is pitiful, I ended up deleting everything except
intList :: [Int]
intList = [ x | x <- [1..]
as a result of nerdraging :o.
I know this was a long post so sorry for the inconvenience but any help would be much appreciated!