Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9280713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:56:29+00:00 2026-06-18T17:56:29+00:00

Given a list like: let list = [1,2,3,4,5,6,7,8,9,10] I am trying to come up

  • 0

Given a list like:

let list = [1,2,3,4,5,6,7,8,9,10]

I am trying to come up with a way to detect if 7,8,9 exists in the list in sequential order and simply printout ‘success’ if it does and ‘fail’ otherwise.

I am trying to accomplish this using zip for the index. Can someone advise if I am on the right track or if there is a better way to accomplish this?

zip [0..] list

And then something like:

[if (snd x)== 
             7 && let index = (fst x) 
               && (snd x)==8 && (fst x)==(index+1) 
               && (snd x)==9 && (fst x)==(index+2) 
               then "success" 
               else "fail" | x <- list]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:56:31+00:00Added an answer on June 18, 2026 at 5:56 pm

    When trying to figure out a list algorithm it’s usually best to start by
    thinking about the special case in the list head. In this case, how would you
    test that the list begins with [7,8,9]?

    beginsWith789 :: [Int] -> Bool
    beginsWith789 (7:8:9:_) = True
    beginsWith789 _         = False
    

    I.e. we can just pattern match to the first three elements. Now to generalize
    this, if we don’t find the subsequence in the list head, we recursively check
    the tail of the list

    contains789 :: [Int] -> Bool
    contains789 (7:8:9:_) = True
    contains789 (_:xs)    = contains789 xs
    contains789 _         = False
    

    Now if we want to further generalize this to find any subsequence, we can use
    the isPrefixOf function from Data.List:

    import Data.List (isPrefixOf)
    
    contains :: Eq a => [a] -> [a] -> Bool
    contains sub lst | sub `isPrefixOf` lst = True
    contains (_:xs)  = contains sub xs
    contains _       = False
    

    We can tidy this up by using any and tails to check if any successively
    shorter tail of the list begins with the given subsequence:

    import Data.List (isPrefixOf, tails)
    
    contains :: Eq a => [a] -> [a] -> Bool
    contains sub = any (sub `isPrefixOf`) . tails
    

    Or, we can simply use the standard library function isInfixOf. 😉

    > import Data.List
    > [7,8,9] `isInfixOf` [1,2,3,4,5,6,7,8,9]
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given A, on the order of 10^20, I'd like to quickly obtain a list
Given a list like this: num = [1, 2, 3, 4, 5] There are
Given a list of elements like so: int[] ia = new int[] { -4,
Given a list of dictionaries like this: x = [ {'name':'a', 'student': 1 ,
Given a list of potential ID's is there a quick way using a single
Let's say we've got a generic list like this: List<string> list = new List<string>()
So let's say I'm trying to get a list of all my users who
I would like to make a method where I could give it a list
Given an R list, I wish to find the index of a given list
The original code was somehow complex, i simplify it as: Given: list of class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.