PFP Laboratory 9

From Marek Běhálek Wiki
Revision as of 12:52, 4 November 2022 by Beh01 (talk | contribs) (Created page with "== Arrays == == Bubble sort == * Create a function, that sorts an array using the bubble sort algorithm. <syntaxhighlight lang="Haskell">bubbleSort :: Array Int Int -> Arra...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Arrays

Bubble sort

  • Create a function, that sorts an array using the bubble sort algorithm.
bubbleSort :: Array Int Int -> Array Int Int
ghci> elems $ bubbleSort $  listArray (0,5) [8,4,9,6,7,1] 
[1,4,6,7,8,9]
module Main (main) where
import System.Random
import System.Environment

myCycle :: Integer -> Integer -> IO Integer
myCycle i c
  | i == 0 = return c
  | otherwise = do 
    x <- randomRIO (0, 1.0) :: IO Double
    y <- randomRIO (0, 1.0) :: IO Double
    myCycle (i-1) (if sqrt (x*x + y*y) <= 1 then c+1 else c)

main :: IO ()
main = do 
    [arg] <- getArgs
    let number = read arg::Integer
    inside <- myCycle number 0
    let my_pi = (fromIntegral (4*inside) / fromIntegral number) :: Double
    print my_pi