Sequential numbers in a list haskell

前端 未结 3 1137
清酒与你
清酒与你 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:45

    This is a solution:

    consecutiveOnes :: [Int] -> Bool
    consecutiveOnes xs = auxOnes False xs
    
    auxOnes :: Bool -> [Int] -> Bool
    auxOnes b [] = False
    auxOnes b (x:xs) = case (x==1 && b) of {
        True -> True;
        False -> auxOnes (x==1) xs;
    };
    

    Another way would be using the isInfixOf method and asking if [1,1] appears anywhere on your list:

    consecutiveOnes :: [Int] -> Bool
    consecutiveOnes xs = isInfixOf [1,1] xs
    

    The isInfixOf function takes two lists and returns True iff the first list is contained, wholly and intact, anywhere within the second.

    But I'm sure there are many other ways of doing it.

提交回复
热议问题