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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:24:55+00:00 2026-05-22T02:24:55+00:00

For the 99 Haskell questions, specifically the 23rd one, I need to Extract a

  • 0

For the 99 Haskell questions, specifically the 23rd one, I need to

“Extract a given number of randomly selected elements from a list.

Example (in lisp):

(rnd-select '(a b c d e f g h) 3)
(E D A)

“

Which I have implemented like so:

import System.Random
import Control.Monad

removeAt :: [a] -> Int -> [a]
removeAt (x:xs) i
    | i > 0  = x : removeAt xs (i-1)
    | otherwise = xs

rndSelect :: (RandomGen g) => [a] -> Int ->  g -> IO [a]
rndSelect _ 0 _ = return []
rndSelect xs n gen = do
    let (pos, newGen) = randomR (0, length xs - 1) gen
    rest <- rndSelect (removeAt xs pos) (n-1) newGen
    return $ (xs!!pos):rest

-- for an explanation of what this is doing see EXPLANATION below

As far as I can tell this works, but what I’m concerned about are those last two lines. I’m new to this and I don’t know the associated costs of the ‘<-‘ operator is or bouncing in and out of IO repeatedly like I’m doing. Is this efficient, is there a better way to do this that doesn’t involve bouncing IO, or is there no real overheads involved?

Any insight you have is appreciated, since I’ve only recently started learning these more sophisticated concepts in Haskell and haven’t yet gotten used to reasoning about Haskell’s IO system.

EXPLANATION: In order to do this I’ve decided that I should randomly select one element from the list using the randomR function (returns a random number in a given range), and keep doing this recursively until I’ve taken n elements.

I’ve made a couple assumptions about the problem that have lead me to this approach. Firstly I’ve assumed that rndSelect can select a specific element from the list only once, and secondly I’ve assumed that each element should have an equal probability of being picked.

PS: it’s my first question on SO so if I’ve formatted the question poorly feel free to tell me.

  • 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-22T02:24:56+00:00Added an answer on May 22, 2026 at 2:24 am

    You do not need IO for this, since randomR does not require it. What you need to do however, is to thread the random number generator through your computation:

    import System.Random
    import Control.Monad
    
    removeAt :: [a] -> Int -> [a]
    removeAt (x:xs) i
        | i > 0  = x : removeAt xs (i-1)
        | otherwise = xs
    
    
    rndSelect :: (RandomGen t, Num a) => [a1] -> a -> t -> ([a1], t)
    rndSelect _ 0 g = ([],g)
    rndSelect xs n gen =
       let (pos, newGen) = randomR (0, length xs - 1) gen
           (rest,ng)     = rndSelect (removeAt xs pos) (n-1) newGen
       in  ((xs!!pos):rest, ng)
    

    If you’re concerned about overheads going from IO to pure code, don’t be. Instead you can try mwc-random package which will be atleast an order of magnitude faster in this case. Further, you could get additional benefit using any random access data structure instead of list if you have many elements.

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

Sidebar

Related Questions

A bit of a neophyte haskell question, but I came across this example in
In Haskell, there is a function take n list which returns the first n
I'm working on the 99 Haskell questions and saw a solution for finding the
I've been working on Question 67A of 99 Haskell Questions . The question is
I am trying to use this simple function from this page: http://www.haskell.org/haskellwiki/99_questions/Solutions/2 But when
I'm revising for an upcoming Haskell exam and I don't understand one of the
I was reading on perceptrons and trying to implement one in haskell. The algorithm
It seems to me from my experimenting with Haskell, Erlang and Scheme that functional
I am really new to Haskell (Actually I saw Real World Haskell from O'Reilly
This was the #10 of the 99 Haskell questions: (*) Run-length encoding of a

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.