I need a program that checks if the difference between all pairs of elements is in the interval from -2 up to 2 ( >= -2 && < 2). If it is, then return True, else return False. Foe example, [1,2,3] is True, but [1,3,4] is False.
I am using the all function. What is wrong with my if clause?
allfunc (x : xs)
= if all (...) xs
then allfunc xs
else [x] ++ allfunc xs
allfunc _
= []
Or I am doing something completely wrong?
For this, it’s probably easier to use list comprehensions or do-notation.
pairsOfreturns the list of pairs of numbers in the inputlst. For example,pairsOf [1,2,3]results in[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)].Now, you can define the difference between a pair in a one-liner
\(x, y) -> x - yand map that over the list:Now you just have to make sure that each element in
differences lstis between-2and2.Of course, this is just one possible way to do it. There are many other ways as well.