Created
June 7, 2015 11:19
-
-
Save certifiedwaif/d75cdf5005764624da7c to your computer and use it in GitHub Desktop.
Construct a histogram of a million random numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- hw3.hs | |
module Main where | |
import System.Random | |
import Data.Vector.Unboxed hiding (forM_, take, unfoldr, map) | |
import Data.Vector.Unboxed.Mutable hiding (take) | |
import Data.List | |
import Control.Monad.ST (runST) | |
import Control.Monad (forM_) | |
import Prelude hiding (read) | |
main :: IO () | |
main = do | |
seed <- newStdGen | |
let rs = randomlist 1000000 seed | |
print $ histogram $ map (`mod` 10) rs | |
-- Exercise 3 - Histogram | |
histogram :: [Int] -> Vector Int | |
histogram l = runST $ do | |
vec <- new 10 | |
set vec (0 :: Int) | |
forM_ l $ \i -> do | |
val <- read vec i | |
write vec i (val + 1) | |
freeze vec | |
randomlist :: Int -> StdGen -> [Int] | |
randomlist n = take n . unfoldr (Just . random) | |
-- Exercise 1 - Hopscotch | |
-- skips :: [a] -> [[a]] | |
-- skips l = [l] | |
-- -- Exercise 2 - Local maxima | |
-- localMaxima :: [Integer] -> [Integer] | |
-- localMaxima = undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment