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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:32:05+00:00 2026-05-20T07:32:05+00:00

This is probably trivial, and I do have a solution but I’m not happy

  • 0

This is probably trivial, and I do have a solution but I’m not happy with it. Somehow, (much) simpler forms don’t seem to work and it gets messy around the corner cases (either first, or last matching pairs in a row).

To keep it simple, let’s define the matching rule as any two or more numbers that have a difference of two. Example:

> filterTwins [1; 2; 4; 6; 8; 10; 15; 17]
val it : int list = [2; 4; 6; 8; 10; 15; 17]

The code I currently use is this, which just feels sloppy and overweight:

let filterTwins list =
    let func item acc =
        let prevItem, resultList = acc
        match prevItem, resultList with
        | 0, [] 
            -> item, []
        | var, [] when var - 2 = item
            -> item, item::var::resultList
        | var, hd::tl when var - 2 = item && hd <> var
            -> item, item::var::resultList
        | var, _ when var - 2 = item
            -> item, item::resultList
        | _ 
            -> item, resultList

    List.foldBack func list (0, [])
    |> snd

I intended my own original exercise to experiment with List.foldBack, large lists and parallel programming (which went well) but ended up messing with the “easy” part…

Guide through the answers

  • Daniel’s last, 113 characters*, easy to follow, slow
  • Kvb’s 2nd, 106 characters* (if I include the function), easy, but return value requires work
  • Stephen’s 2nd, 397 characters*, long winded and comparably complex, but fastest
  • Abel’s, 155 characters*, based on Daniel’s, allows duplicates (this wasn’t a necessity, btw) and is relatively fast.

There were more answers, but the above were the most distinct, I believe. Hope I didn’t hurt anybody’s feelings by accepting Daniel’s answer as solution: each and every one solution deserves to be the selected answer(!).

* counting done with function names as one character

  • 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-05-20T07:32:05+00:00Added an answer on May 20, 2026 at 7:32 am

    Would this do what you want?

    let filterTwins l = 
        let rec filter l acc flag =
            match l with
            | [] -> List.rev acc
            | a :: b :: rest when b - 2 = a -> 
                filter (b::rest) (if flag then b::acc else b::a::acc) true
            | _ :: t -> filter t acc false
        filter l [] false
    

    This is terribly inefficient, but here’s another approach using more built-in functions:

    let filterTwinsSimple l =
        l 
        |> Seq.pairwise 
        |> Seq.filter (fun (a, b) -> b - 2 = a)
        |> Seq.collect (fun (a, b) -> [a; b])
        |> Seq.distinct
        |> Seq.toList
    

    Maybe slightly better:

    let filterTwinsSimple l =
        seq { 
            for (a, b) in Seq.pairwise l do
                if b - 2 = a then 
                    yield a
                    yield b 
        }
        |> Seq.distinct
        |> Seq.toList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is probably rather trivial but I have had a look at
I'm new and this is probably very trivial but I have the following method
This is probably a very trivial question, but.. Assuming we have a variable 'a'
This is probably quite trivial but.... I have a quite complicated linq query that
Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
This is probably a trivial question for most but here goes... I am using
This is probably close to trivial, but I can't find it. How can I
This is trivial, probably silly, but I need to understand what state cout is
I know this probably really simple but Im not sure what im doing wrong...
Strange and probably trivial problem. I have three projects in one solution (.NET 2.0,

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.