I am trying to write a list function that takes a simple list and feeds back a list of lists, all of the latter's elements having the same relationship with the previous one.
In more concrete terms, the function should be doing this:
- take a list;
let xs = [1,2,3,4,5,6,8,9,10] - look at two elements from the head and, if the second one equals to the first one plus one (i.e.,
xs!!0 = xs!!1 - 1), create a list-within-a-list out of them. - The list takes elements while the last of them have the same relationship with the element, newly fed from the main list. When there is a hiatus, the sublist closes but the function should make a new sublist, based on the same condition all the way through.
- So, the final outcome should be,
[[1,2,3,4,5,6],[8,9,10]]The absent 7 divides the main list into two sublists. Both of them are arithmetic progressions with the common difference being 1.
After reading Learn You a Haskell for Great Good till chapter 7, I thought I really had a great idea and tried and ingloriously failed. Help is most welcome, please!
ghci> filter (\x y -> x + 1 == y) xs "<"interactive">":1:8: The lambda expression `\ x y -> x + 1 == y' has two arguments, but its type `a -> Bool' has only one In the first argument of `filter', namely `(\ x y -> x + 1 == y)' In the expression: filter (\ x y -> x + 1 == y) xs In the definition of `it': it = filter (\ x y -> x + 1 == y) xs
ghci> let xs = [1,2,3,4,8,5,10] ghci> [x | x <- myGroupBy (\x y -> x*2==y) xs, length x > 1] [[1,2],[4,8],[5,10]]never mind.