tags:

views:

64

answers:

4

with the signature diffFolge:: (Integer, Integer) -> [Integer], which applied to the argument pair (m, n), m, n> 0, the episode of the difference values of m and n supplies. The first element of the Result list is m, the last but one element is always is greater than 0 and the last element either 0 or a value strictly less than 0

i write it so

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

but i take as result [5,4,3,2,1,0,-1] by first one how can i correct this

+3  A: 

Haskell's [a..b] syntax returns a list including b if possible.

You could use [m, m-n .. 1-n], such that -n is excluded.

(BTW, n-2*n == -n)

KennyTM
this is true but for example when i give this as example diffFolge (5,2) output muss so [5,3,1,-1]
marco
@ifan: See update.
KennyTM
A: 

Not sure I understand what you want, but I am guessing this is it:

diffFolge  (m,n) | m <= 0    = [m]
                 | otherwise = m : diffFolge (m-n,n)
HaskellElephant
that was the answer what m i looking for thank you...
marco
A: 

To me it also occurs that you've asserted m>=n So here's a quick Idea:

diffFolge :: (Integer, Integer) -> [Integer]
diffFolge (m,n)
    | m <= 0 || n <= 0 = []
    | n > m = diffFolge (n,m)    -- | Only if you want this case
    | otherwise = takeWhile (>= 0) $ iterate (\x->x-n) m
Jakob Runge
thank you for this
marco
A: 

Sometimes, it pays to do just a little bit of math. :)

diffFolge (m, n) = [m, m-n .. 1-n]

Also, it's unusual to use a tuple for multiple arguments to a function in Haskell. I would be more likely to write

df m n = [m, m-n .. 1-n]

And then, if for some reason I really need a function taking a tuple, I would write:

diffFolge = uncurry df
Yitz
Ah, I missed KennyTM's answer which already pointed this out.
Yitz