Skip to content

Instantly share code, notes, and snippets.

@hvariant
Last active October 27, 2019 05:40
Show Gist options
  • Select an option

  • Save hvariant/e155eb7f724cf4e2304eea557eec4f97 to your computer and use it in GitHub Desktop.

Select an option

Save hvariant/e155eb7f724cf4e2304eea557eec4f97 to your computer and use it in GitHub Desktop.
rnd_permu :: [a] -> IO [a]
rnd_permu xs = do
arr <- A.newListArray (1,l) xs
forM [1..l] $ \i -> do
j <- randomRIO (1, i)
vi <- (A.readArray arr i)
vj <- (A.readArray arr j)
A.writeArray arr j vi
A.writeArray arr i vj
pure vj
where
l = length xs
rnd_permu :: [a] -> IO [a]
rnd_permu xs = do
arr <- newArray l xs
forM [1..l] $ \i -> do
j <- randomRIO (1, i)
vi <- (A.readArray arr i)
vj <- (A.readArray arr j)
A.writeArray arr j vi
A.writeArray arr i vj
pure vj
where
l = length xs
newArray :: Int -> [a] -> IO (IOArray Int a)
newArray n xs = A.newListArray (1,n) xs
@hvariant
Copy link
Author

hvariant commented Oct 27, 2019

error:

    • Ambiguous type variable ‘a0’ arising from a use of ‘newListArray’
      prevents the constraint ‘(MArray a0 a IO)’ from being solved.
      Relevant bindings include
        xs :: [a] (bound at Q21_28.hs:41:11)
        rnd_permu :: [a] -> IO [a] (bound at Q21_28.hs:41:1)
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instance exist:
        instance MArray IOArray e IO -- Defined in ‘Data.Array.Base’
    • In a stmt of a 'do' block: arr <- newListArray (1, l) xs
      In the expression:
        do arr <- newListArray (1, l) xs
           forM [1 .. l] $ \ i -> do ...
      In an equation for ‘rnd_permu’:
          rnd_permu xs
            = do arr <- newListArray (1, l) xs
                 forM [1 .. l] $ \ i -> ...
            where
                l = length xs
   |
42 |   arr <- A.newListArray (1,l) xs
   |          ^^^^^^^^^^^^^^^^^^^^^^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment