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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:15:23+00:00 2026-05-16T05:15:23+00:00

I am new to Haskell and I wonder how/if I can make this code

  • 0

I am new to Haskell and I wonder how/if I can make this code more efficient and tidy. It seems unnecessarily long and untidy.

My script generates a list of 10 averages of 10 coin flips.

import Data.List
import System.Random

type Rand a = StdGen -> Maybe (a,StdGen)

output = do
    gen <- newStdGen
    return $ distBernoulli 10 10 gen

distBernoulli :: Int -> Int -> StdGen -> [Double]
distBernoulli m n gen = [fromIntegral (sum x) / fromIntegral (length x) | x <- lst]
    where lst = splitList (randomList (n*m) gen) n

splitList :: [Int] -> Int -> [[Int]]
splitList [] n = []
splitList lst n = take n lst : splitList (drop n lst) n

randomList :: Int -> StdGen -> [Int]
randomList n = take n . unfoldr trialBernoulli

trialBernoulli :: Rand Int
trialBernoulli gen = Just ((2*x)-1,y)
                 where (x,y) = randomR (0,1) gen

Any help would be appreciated, thanks.

  • 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-16T05:15:23+00:00Added an answer on May 16, 2026 at 5:15 am

    I’d tackle this problem in a slightly different way. First I’d define a function that would give me an infinite sampling of flips from a Bernoulli distribution with success probability p:

    flips :: Double -> StdGen -> [Bool]
    flips p = map (< p) . randoms
    

    Then I’d write distBernoulli as follows:

    distBernoulli :: Int -> Int -> StdGen -> [Double]
    distBernoulli m n = take m . map avg . splitEvery n . map val . flips 0.5
      where
        val True = 1
        val False = -1
        avg = (/ fromIntegral n) . sum
    

    I think this matches your definition of distBernoulli:

    *Main> distBernoulli 10 10 $ mkStdGen 0
    [-0.2,0.4,0.4,0.0,0.0,0.2,0.0,0.6,0.2,0.0]
    

    (Note that I’m using splitEvery from the handy split package, so you’d have to install the package and add import Data.List.Split (splitEvery) to your imports.)

    This approach is slightly more general, and I think a little neater, but really the main difference is just that I’m using randoms and splitEvery.

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

Sidebar

Related Questions

Im new to Haskell!! I wrote this code: import Data.List inputIndex :: [String] ->
I'm new to Haskell, so am sorry if this is incredibly obvious... I have
I am new to functional programming, and now learn Haskell. As an exercise I
New to javascript/jquery and having a hard time with using this or $(this) to
New to WCF, but familiar with COM+ - can I wrap a WCF service
In new C++ code, I tend to use the C++ iostream library instead of
I just started a new Haskell project and wanted to set up a good
New to Haskell and have a stumbling block. I'm trying to filter a list
I am new to Haskell and facing a "cannot construct infinite type" error that
I am really new to Haskell (Actually I saw Real World Haskell from O'Reilly

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.