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 8702567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:41:04+00:00 2026-06-13T02:41:04+00:00

I want to make a module in Mathematica that return true if the first

  • 0

I want to make a module in Mathematica that return true if the first list (L1) is in the second list (L2) assuming that the length of L2 is greater than the length of L1.
I did it in this way, the problem is that always return False and I don’t know why.
EDIT: I have solved a problem: I wrote “if” instead of “If”. Now I get an infinitely loop.

isSegment[L1_List, L2_List] := Module[{i, j},   For[i = 1, i + Length[L1] - 1 <= Length[L2],
       For[j = 1, j <= Length[L1],
    If[L2[[i + j - 1]] != L1[[j]], Break;];
    j++;
    ];
       If[j == Length[L1] + 1,
    Return[ True];];
       i++;    ];   Return [False];   ]
  • 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-13T02:41:05+00:00Added an answer on June 13, 2026 at 2:41 am

    This is likely to be one of the cleanest and fastest general methods available:

    isSegment[{L1__}, L2_List] := MatchQ[L2, {___, L1, ___}]
    
    isSegment[{3, 4, 5}, Range@10]
    
    True
    

    For a list of all Real or Integer values you could adapt the methods shown here for maximum speed.


    user writes:

    Thanks, it is a good way, but what I want is to correct my code not to have a new one because it is an exercise for school and in the statement it is said that we must use For loops.

    It appears that there is confusion over the syntax of Break[] and the function of Return. To correct the code I replaced Break with Break[] and Return with Throw & Catch.

    isSegment[L1_List, L2_List] :=
     Catch @ Module[{i, j},
       For[i = 1, i + Length[L1] - 1 <= Length[L2],
        For[j = 1, j <= Length[L1], 
         If[L2[[i + j - 1]] != L1[[j]], Break[];]; j++;];
        If[j == Length[L1] + 1, Throw[True];];
        i++;]; Throw[False];
       ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make search module just like android keypad that gives the list
I want to make a module in mathematica that returns if an automaton is
I want to make my first release of a multi-module maven project using the
For my NodeJS project, I want to make a simple module that will give
I want to make a application in php that will have a chatting module
want to make small python module that can get data from a database. I
I am traversing a HTML document using javascript DOM. I want make a list
I'm trying make my first python app. I want make simple email sender form.
I'm want to make code that reads assembly instructions (x86 only) and recreates them
for the purpose of a module that I began to realize, I want to

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.