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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:55:57+00:00 2026-05-13T11:55:57+00:00

(I am aware of this question , but it relates to sequences, which is

  • 0

(I am aware of this question, but it relates to sequences, which is not my problem here)

Given this input (for example):

let testlist = 
    [  
       "*text1";
       "*text2";
       "text3";
       "text4";
       "*text5";
       "*text6";
       "*text7"
    ]

let pred (s:string) = s.StartsWith("*")

I would like to be able to call MyFunc pred testlist and get this output:

[
    ["*text1";"*text2"];
    ["*text5";"*text6";"*text7"]
]

This is my current solution, but I don’t really like the nested List.revs (ignore the fact that it takes Seq as input)

let shunt pred sq =
    let shunter (prevpick, acc) (pick, a) = 
        match pick, prevpick with
        | (true, true)  -> (true, (a :: (List.hd acc)) :: (List.tl acc))
        | (false, _)    -> (false, acc)
        | (true, _)     -> (true, [a] :: acc)

    sq 
        |> Seq.map (fun a -> (pred a, a))
        |> Seq.fold shunter (false, []) 
        |> snd
        |> List.map List.rev
        |> List.rev
  • 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-13T11:55:57+00:00Added an answer on May 13, 2026 at 11:55 am

    Edit: rev-less version using foldBack added below.

    Here’s some code that uses lists and tail-recursion:

    //divides a list L into chunks for which all elements match pred
    let divide pred L =
        let rec aux buf acc L =
            match L,buf with
            //no more input and an empty buffer -> return acc
            | [],[] -> List.rev acc 
            //no more input and a non-empty buffer -> return acc + rest of buffer
            | [],buf -> List.rev (List.rev buf :: acc) 
            //found something that matches pred: put it in the buffer and go to next in list
            | h::t,buf when pred h -> aux (h::buf) acc t
            //found something that doesn't match pred. Continue but don't add an empty buffer to acc
            | h::t,[] -> aux [] acc t
            //found input that doesn't match pred. Add buffer to acc and continue with an empty buffer
            | h::t,buf -> aux [] (List.rev buf :: acc) t
        aux [] [] L
    

    usage:

    > divide pred testlist;;
    val it : string list list =
      [["*text1"; "*text2"]; ["*text5"; "*text6"; "*text7"]]
    

    Using a list as data structure for a buffer means that it always needs to be reversed when outputting the contents. This may not be a problem if individual chunks are modestly sized. If speed/efficiency becomes an issue, you could use a Queue<'a> or a `List<‘a>’ for the buffers, for which appending is fast. But using these data structures instead of lists also means that you lose the powerful list pattern matching. In my opinion, being able to pattern match lists outweighs the presence of a few List.rev calls.

    Here’s a streaming version that outputs the result one block at a time. This avoids the List.rev on the accumulator in the previous example:

    let dividestream pred L =
        let rec aux buf L =
            seq { match L, buf with
                  | [],[] -> ()
                  | [],buf -> yield List.rev buf
                  | h::t,buf when pred h -> yield! aux (h::buf) t
                  | h::t,[] -> yield! aux [] t
                  | h::t,buf -> yield List.rev buf
                                yield! aux [] t }
        aux [] L
    

    This streaming version avoids the List.rev on the accumulator. Using List.foldBack can be used to avoid reversing the accumulated chunks as well.

    update: here’s a version using foldBack

    //divides a list L into chunks for which all elements match pred
    let divide2 pred L =
        let f x (acc,buf) =
            match pred x,buf with
            | true,buf -> (acc,x::buf)
            | false,[] -> (acc,[])
            | false,buf -> (buf::acc,[])
    
        let rest,remainingBuffer = List.foldBack f L ([],[])
        match remainingBuffer with
        | [] -> rest
        | buf -> buf :: rest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: sorry as this is not a programming question but i am not aware
I aware that this will be a less programming question, but still... How can
This question is not language-aware. I wanna know how can I recognize a returning
In this question , I was made aware of glob()'s GLOB_BRACE option that allows
In the answer to this question ovanes states: Please be aware that boost::lexical_cast is
I'm aware that this is possible with the os module's os.system(color) function, but this
I'm aware of using syntax like this: (something) ? TRUE : FALSE But what
I am aware of NSStringFromClass. My question relates to the situation where the same
I am aware that this could be seen as subjective but this is definitely
Background: I am aware of this SO question about Transactional NTFS (TxF) and this

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.