Questions tagged [starray]

Mutable boxed and unboxed arrays in the `ST` monad.

Mutable boxed and unboxed arrays in the ST monad.

ST is a monad in which a limited type of side effects are allowed, namely mutable references and mutable arrays. Thus it allows you to implement functions which are pure as seen from the outside world, but which use mutation internally.

7 questions
3
votes
1 answer

Haskell ST Monad: No instance for (MArray (STArray s) Int (ST s1))

I have been learning Haskell for the past month or two, and recently solved this coding problem. The additional challenge was to do the task without extra space and in linear time, which I did not think would be possible to do in a purely functional…
Zafer Cesur
  • 497
  • 2
  • 10
3
votes
3 answers

How can I implement a Fisher-Yates shuffle in Scala without side effects?

I want to implement the Fisher-Yates algorithm (an in-place array shuffle) without side effects by using an STArray for the local mutation effects, and a functional random number generator type RNG[A] = State[Seed,A] to produce the random integers…
arya
  • 946
  • 5
  • 14
3
votes
1 answer

Having ST(U)Arrays in a data structure?

What do I have to do to make GHC accept this code: {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} module STTest where import Data.Array.ST import Control.Monad.ST.Strict as S import Control.Monad.ST.Lazy as L -- ST monad arrays…
integer
  • 1,075
  • 7
  • 12
2
votes
1 answer

How to improve this algorithm by 1)use arrays, 2)avoid list concatenation (lazy lists?)?

I tried to learn how the STArray works, but I couldn't. (Doc is poor, or at least the one I found). Any way, I have the next algorithm, but it uses a lot of !!, which is slow. How can I convert it to use the STArray monad? -- The Algorithm prints…
Lay González
  • 2,901
  • 21
  • 41
2
votes
1 answer

Multiple updates in ST-Monad

I want to learn using the ST-Monad. Therefore I want to rewrite a bit of code computing for every integer – up to a limit – the list of all its proper divisors. The result should be an array and the entry of index 'n' should be the list of it's…
user2292040
  • 305
  • 1
  • 7
2
votes
3 answers

STArray and stack overflow

I am struggling to understand why the following attempts to find a minimum element in STArray lead to stack space overflow when compiled with ghc (7.4.1, regardless of -O level), but work fine in ghci: import Control.Monad import…
Ed'ka
  • 6,595
  • 29
  • 30
1
vote
1 answer

Two almost identical functions using STArray: why does one requires FlexibleContexts, and the other does not?

Consider the Haskell functions test :: ST s [Int] test = do arr <- newListArray (0,9) [0..9] :: ST s (STArray s Int Int) let f i = do writeArray arr i (2*i) readArray arr i forM [1,2] f and test' :: ST s [Int] test' =…
Mark Wildon
  • 217
  • 1
  • 5