Search code examples
functional-programmingelm

Is there a way to find if a sequence of two chars are found in a list only if they are consecutive?


I am currently working in the elm syntax. An example would be like this:

(Sequence ('a') ('b')) ('c') ['a', 'b', 'c', 'd'] . In this example, i only test if the elements 'a', 'b', 'c' are members of the list. If yes, then i partition it and obtain (['a','b','c'],['d'])

I encountered problems in the following case:

 (Sequence ('a') ('b')) ('c') ['a', 'b', 'c', 'a']

obtaining the result : (['a','b','c','a'],[])

My question is: what condition should i put such that the elements 'a' and 'b' must be consecutive avoiding the case when they are matched alone?


Solution

  • This answer assumes that if you have Sequence 'a' 'b' 'c' and test it against the list ['a', 'b', 'c', 'a'], you want to receive the result (['a', 'b', 'c'], ['a']) (as asked in this comment).

    In pseudo-code:

    1. Split the list into two, list1 and list2. list1 should have the same length as your sequence. Elm provides List.take and List.drop for that
    2. Convert your sequence into a list list_sequence with a helper function
    3. Test if list1 and list_sequence are equal
    4. If they are, return the tuple (list1, list2)

    And here is the actual Elm code: https://ellie-app.com/bjBLns4dKkra1