Jump to content

Haskell Find Consecutive Numbers in a List

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Guenhwyvar

Guenhwyvar

    Newbie

  • Members
  • Pip
  • 3 posts
Hiya,

I really need help ASAP on the one. I have a list (_,Index) where the index's are '2, 4,6, 7, 9, 10, 13,' and I need to return all the index's that are consecutive.There is no way to loop properly in haskell and I cant think of a way. Ie 6 7,9 and 10 are consecutive so I want to return (_,6),(_7),(_,9),(_10). .

Thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You'll have to use a recursive solution, where you return the current portion along with whatever the next recursive call returns.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
List comprehension is pretty easy in haskell unless I'm misunderstanding your question

ghci> let indexs = [2, 4, 6, 7, 9, 10, 13]
ghci> [(x, y) | x <- indexs, y <- tail indexs, (x + 1) == y]

[(6,7),(9,10)]

"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users