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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:37:42+00:00 2026-05-27T18:37:42+00:00

Given a list, I’d like to divide it into clusters using a boundary function.

  • 0

Given a list, I’d like to divide it into clusters using a “boundary function”. Such function would take two consecutive elements of the list and decide whether or not they should belong to the same cluster.

So essentially, I want something like this:

clusterBy :: (a -> a -> Bool) -> [a] -> [[a]]

ghci> farAway x y = abs (x - y) > 10
ghci> clusterBy farAway [1, 4, 18, 23, 1, 17, 21, 12, 30, 39, 48]
[[1, 4], [18, 23], [1], [17, 21, 12], [30, 39, 48]]

Looking up it this type declaration in Hoogle yielded only groupBy which isn’t exactly what I need (it doesn’t take order of elements into account).

Is there any library function that does something similar? Or alternatively: how it could be implemented cleanly, i.e. without resorting to tail-recursive loop?

EDIT: For reference, the implementation I managed to cook up is the following:

clusterBy :: (a -> a -> Bool) -> [a] -> [[a]]
clusterBy isBoundary list = 
loop list [] []
    where
        loop (first:rest) [] result = loop rest [first] result
        loop list@(next:rest) cluster result =
            if isBoundary (head cluster) next then
                loop list [] ((reverse cluster):result)
            else
                loop rest (next:cluster) result
        loop [] cluster@(_:_) result = cluster:result
        loop _ _ result = result
  • 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-27T18:37:43+00:00Added an answer on May 27, 2026 at 6:37 pm

    Is this what you want?

    clusterBy :: (a -> a -> Bool) -> [a] -> [[a]]
    clusterBy isDifferentCluster = foldr f []
      where f x (ys @ (y : _) : yss) | not (isDifferentCluster x y) = (x : ys) : yss
            f x                 yss                                 = [x]      : yss
    

    The sense of the function argument should probably be reversed (i.e. return true when its arguments should be in the same cluster and false otherwise), but I’ve given it in the form you asked for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list of urls, I would like to check that each url: Returns
Given a list of locations such as <td>El Cerrito, CA</td> <td>Corvallis, OR</td> <td>Morganton, NC</td>
Given a list, how would I select a new list, containing a slice of
Given a list of objects, I am needing to transform it into a dataset
Given a list like this: num = [1, 2, 3, 4, 5] There are
I coded a function to enumerate all permutations of a given list. What do
I have a time-series like function which gives an output based on a list
I'am tring to retrieve from my database (using JPA) for a given list of
How to define a rotates function that generates all rotations of the given list?
I have a function that returns a comparison matrix from a given list: def

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.