mapAccumL - find max in-sequence subsequence

I need to find the length of the longest in-sequence section of a list of ints...I am guessing something like mapAccumL is the way but not sure how. --this doesn't work Prelude List> mapAccumL (\x y -> if y==(x+1) then (y, x) else (y,0)) 0 $ sort $ nub [9, 1, 2, 7, 3, 4, 5, 6,5] (9,[0,1,2,3,4,5,6,0]) -- View this message in context: http://www.nabble.com/mapAccumL---find-max-in-sequence-subsequence-tf2531704... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On 10/28/06, jim burton
I need to find the length of the longest in-sequence section of a list of ints...I am guessing something like mapAccumL is the way but not sure how.
--this doesn't work Prelude List> mapAccumL (\x y -> if y==(x+1) then (y, x) else (y,0)) 0 $ sort $ nub [9, 1, 2, 7, 3, 4, 5, 6,5] (9,[0,1,2,3,4,5,6,0])
I'm not sure I completely understand what you want, and if it needs to be "cute" (i.e. some clever one liner usage of a library function). But here's my "get-the-job-done-solution" (assuming I understood what you want): import Data.List import Data.Ord longestInSequence :: (Enum a) => [a] -> Int longestInSequence = maximum . map (length . takeInSeq) . tails takeInSeq [] = [] takeInSeq [x] = [x] takeInSeq (x:y:xs) | fromEnum (succ x) == fromEnum y = x : takeInSeq (y:xs) | otherwise = takeInSeq (x:xs) /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862

Sebastian Sylvan-2 wrote:
I'm not sure I completely understand what you want, and if it needs to be "cute" (i.e. some clever one liner usage of a library function). But here's my "get-the-job-done-solution" (assuming I understood what you want):
import Data.List import Data.Ord
longestInSequence :: (Enum a) => [a] -> Int longestInSequence = maximum . map (length . takeInSeq) . tails
takeInSeq [] = [] takeInSeq [x] = [x] takeInSeq (x:y:xs) | fromEnum (succ x) == fromEnum y = x : takeInSeq (y:xs) | otherwise = takeInSeq (x:xs)
/S
Thanks, that's what I was looking for - and it doesn't need to be 'cute'! -- View this message in context: http://www.nabble.com/mapAccumL---find-max-in-sequence-subsequence-tf2531704... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

It's a pity that groupBy isn't defined a little differently: -- @'groupBy' rel xs@ returns the shortest list of lists such that -- -- * the concatenation of the lists is @xs@, and -- -- * @rel@ is 'True' for each consecutive pair of elements in a sublist. -- groupBy :: (a -> a -> Bool) -> [a] -> [[a]] groupBy rel [] = [] groupBy rel (x:xs) = (x:ys) : groupBy rel zs where (ys,zs) = groupByAux x xs groupByAux x0 (x:xs) | rel x0 x = (x:ys, zs) where (ys,zs) = groupByAux x xs groupByAux y xs = ([], xs) That's equivalent to the existing definition if rel is symmetric and transitive, but also useful in other cases, e.g. groupBy (<=) would generate a list of "runs", and the solution to the problem here would be longestInSequence :: (Enum a, Eq a) => [a] -> Int longestInSequence = maximum . map length . groupBy adjacent where adjacent x y = succ x == y
participants (3)
-
jim burton
-
Ross Paterson
-
Sebastian Sylvan