Sequential numbers in a list haskell

前端 未结 3 1136
清酒与你
清酒与你 2021-01-26 11:55

I am new to haskell and I was attempting a few coding problems that I previously completed for java, however the following problem has me stumped.

Basically the idea is

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-26 12:58

    You could also do it this way:

    consecutiveOnes [] = False
    consecutiveOnes xs = any (== (1,1)) $ zip xs (tail xs)
    

    If

     xs == [0,1,1,2,3,4]
    

    Then

    tail xs == [1,1,2,3,4]
    

    Zipping them together you get a list of pairs, where each pair is an element of the list and the element after it.

    zip xs (tail xs) == [(0,1),(1,1),(1,2),(2,3),(3,4)]
    

提交回复
热议问题