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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:09:01+00:00 2026-06-05T06:09:01+00:00

The function search below searches for two inputs which have the same output under

  • 0

The function search below searches for two inputs which have the same output under some function. During the search it iterates over the input list xs twice, and this input list could be very large, e.g. [0..1000000000]. I’d rather use memory for storing the HashSet created by collision rather than storing the elements of xs, and my understanding is that even though xs could be lazily computed it would be kept around in case it was needed for the call to find.

Questions:

  • is this understanding correct?
  • if I keep it as a list is there a way I can have xs recomputed if it is passed to find?
  • is there an alternative data structure I can use for xs which allows me to control the space used? xs is just used to specify which inputs to check.

Note that there are no type restrictions on xs – it can be a collection of any type.

import Data.HashSet as Set
import Data.Hashable
import Data.List

search :: (Hashable b, Eq b) => (a->b) -> [a] -> Maybe (a,a)
search h xs =
  do x0 <- collision h xs
     let h0 = h x0
     x1 <- find (\x -> (h x) == h0) xs
     return (x0,x1)

collision :: (Hashable b, Eq b) => (a->b) -> [a] -> Maybe a
collision h xs = go Set.empty xs
  where
    go s [] = Nothing
    go s (x:xs) =
      if y `Set.member` s
        then Just x
        else go (Set.insert y s) xs
      where y = h x

main = print $ search (\x -> x `mod` 21)  ([10,20..2100] :: [Int])
  • 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-06-05T06:09:02+00:00Added an answer on June 5, 2026 at 6:09 am

    I answered basically this question here: https://stackoverflow.com/a/6209279/371753

    Here’s the relevant code.

    import Data.Stream.Branching(Stream(..))
    import qualified Data.Stream.Branching as S
    import Control.Arrow
    import Control.Applicative
    import Data.List
    
    data UM s a = UM (s -> Maybe a) deriving Functor
    type UStream s a = Stream (UM s) a
    
    runUM s (UM f) = f s
    liftUM x = UM $ const (Just x)
    nullUM = UM $ const Nothing
    
    buildUStream :: Int -> Int -> Stream (UM ()) Int
    buildUStream start end = S.unfold (\x -> (x, go x)) start
        where go x
               | x < end = liftUM (x + 1)
               | otherwise = nullUM
    
    usToList x = unfoldr (\um -> (S.head &&& S.tail) <$> runUM () um) x
    

    Long story short, instead of passing around a list, pass around a data type that describes how to generate a list. Now you can write functions directly over the stream, or you can use the usToList function to use the list functions you already have.

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

Sidebar

Related Questions

I have a search function in which i am returning all the records of
Below is the function which is called to show the autocomplete in a search
My function to implement the search is below. The issue I have is I
I have an array returned by the function Search.all : => [#<Search id: 7,
I have a similar code snippet like this class Search { public function search($for,
I have an active directory search function: Function GetAdInfo(ByVal ADDN As String, ByVal ADCommonName
I have a binary search function that will search for a word in an
I have a function that searches for a string inside a text file. I
My function is pretty much a standard search function... I've included it below. In
I have a search action in Zend Framework. And code is: function searchAction() {

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.