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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:27:48+00:00 2026-06-09T06:27:48+00:00

I am a newbie to haskell and functional programming. This might be a very

  • 0

I am a newbie to haskell and functional programming. This might be a very simple thing, but I haven’t found an answer while searching.

I have this function main here:

main :: IO ()
main = print =<< (`rnd_select` 6) =<< readNumbers

readNumbers :: IO [(Int,Int,Int)]
readNumbers = makeTriples . map rInt . words <$> readFile "somefile"

rInt :: String -> Int
rInt = read

makeTriples :: [a] -> [(a,a,a)]
makeTriples [] = []
makeTriples (x:y:z:zs) = (x,y,z) : makeTriples zs

rnd_select :: [a] -> Int -> IO [a]
rnd_select _  0 = return []
rnd_select [] _ = return []
rnd_select xs count = do r <- randomRIO (0, (length xs)-1)
                         rest <- rnd_select (removeAt (r+1) xs) (count-1)
                         return ((xs!!r) : rest)

removeAt :: Int -> [a] -> [a]
removeAt _ [] = []
removeAt 1 (x:xs) = xs
removeAt k (x:xs) = let r = removeAt (k - 1) xs in x:r

This is wrapped in the IO-monad and that is what makes it hard when i want to use functions on the values.

Here i use the bind function to apply the input to the rnd_select function:

(`rnd_select` 6) =<< readNumbers

But in order to do that I have to partially apply it to a value.

I don’t think it looks very good and I don’t know how I would do it if the function had more variables.

So I want to know if there is a nicer way to apply values to functions such as these?

  • 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-09T06:27:49+00:00Added an answer on June 9, 2026 at 6:27 am

    Apart from defining the function such that the argument left out is the last argument, so that you can write

    foobar = baz . foo 5 17 29 . bar
    

    you can use a lambda to leave out arguments other than the last,

    foobar = baz . (\x -> foo 5 17 x 29 41) . bar
    

    but for the case of just two arguments, flip is a nice alternative,

    main = print =<< flip rnd_select 6 =<< readNumbers
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a haskell newbie and I couldn't find an answer to this question. Can
this may be a stupid question but i couldn't find answer anywhere. I'm a
I am a newbie in Haskell and hope this question is not silly. I
Newbie question here but for some reason I cant figure this out. I have
I'm a Haskell newbie, so please excuse me if you find this question trivial:
I'm working on learning some Haskell (please excuse the newbie error)- This routine errors
I'm a functional programming newbie. I'd like to know how to implement numpy.where() in
I'm a complete newbie at Haskell. I'm trying to compile this Haskell file I've
I'm a newbie to Haskell, got stuck on a simple question: aaa :: [[(Char,
I'm newbie to haskell. while wxFreeChart page in wxCode says it supports all wx

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.