I need to make a function with the signature diffFolge :: (Integer, Integer) -> [Integer], which when applied to the argument pair (m, n) (m, n> 0) returns a descending list of numbers, the difference between the numbers beeing n. The first element of the
Result list is m, the last but one element is always greater than 0 and the last element
either 0 or a value strictly less than 0.
I write it as follows:
diffFolge :: (Integer,Integer) -> [Integer]
diffFolge (m,n) = if m > 0 && n > 0 then [m,m-n..n-2*n] else []
example
input : diffFolge (5,1)
output : [5,4,3,2,1,0]
example
input : diffFolge (5,2)
output :[5,3,1,-1] ---> This is true by my code
However, with the input given in the first example my function returns [5,4,3,2,1,0,-1]. How can I correct this?
Not sure I understand what you want, but I am guessing this is it:
The error in your code is really the last element in your sequence,
n - 2*n, notethat this is equal to
-n, so your list does not stop until you get an element which is strictly smaller than-n, but in the case where your list includes 0, the next to last element is0and the last element is-n. To fix this you can simply add1to it:[m,m-n..1-n]. Then if the list contains0,-nis excluded, and if it does not contain0, the last element is between0and1-n.