I’m Haskell newbie and I have the little problem.
I’m trying to write a function that recognizes if list is arithmetic sequence or not.
I have this not working code:
isArithmSeq :: [Int] -> Bool
isArithmSeq [] = False;
isArithmSeq [x] = False;
isArithmSeq [x,y] = True;
isArithmSeq (x:y:xs) = (sum (x:y:xs)) == (sum [x,y..(last xs)])
I have no idea how to make it work. Can anyone help me correct this?
Thx.
Try this:
You’ve defined a nice base case for a recursive definition. Now, you just need to check that the difference between successive elements is always the same. The reason that
didn’t work is because the sum is not the only requirement for an arithmetic sequence: