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

  • Home
  • SEARCH
  • 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

This probably has a simple answer, but I must not have had enough coffee
This is probably quite trivial but.... I have a quite complicated linq query that
I'm new and this is probably very trivial but I have the following method
This probably sounds really stupid but I have noo idea how to implement jquery's
This is a slightly odd problem, which means the solution is probably something trivial
Probably a trivial question, but I want to get the best possible solution. Problem:
Hey, I am guessing that this is probably fairly trivial, but I am having
this is probably an essentially trivial thing, but it somewhat escapes me, thus far..
This probably has a really obvious answer, but I can't seem to find it.
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my

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.