[Haskell] How to use monads?

359 views
Skip to first unread message

rincewind

unread,
Jan 14, 2007, 8:51:34 AM1/14/07
to
I'm struggling with monads, and my brain hurts...

How somebody show me how I can implement an in-place modification of an
Array with monads? Say, I have to write the swap function (I'm not sure
about the correctness of this function declaration, but you should get
the idea):

swap ar i j :: Array a -> Ix -> Ix

Simon Richard Clarkstone

unread,
Jan 14, 2007, 12:23:01 PM1/14/07
to

I have cross-posted this to comp.lang.haskell, which is for just this
sort of thing.

Try looking at Control.Monad.ST, Data.Array.ST, and Data.Array.MArray.

The typeclasses used there are complicated, but your signature seems
wrong to me.

--
Simon Richard Clarkstone: s.r.cl?rkst?n?@durham.ac.uk/s?m?n.cl?rkst?n?@
hotmail.com ### "I have a spelling chequer / it came with my PC /
it plainly marks for my revue / Mistake's I cannot sea" ...
by: John Brophy (at: http://www.cfwf.ca/farmj/fjjun96/)

Message has been deleted

Simon Richard Clarkstone

unread,
Jan 14, 2007, 12:24:30 PM1/14/07
to

I have cross-posted this to comp.lang.haskell, which is for just this
sort of thing.

Try looking at Control.Monad.ST, Data.Array.ST, and Data.Array.MArray.

The typeclasses used there are complicated, but your signature seems
wrong to me.

(I hope my cancel message worked.)

Albert Lai

unread,
Jan 14, 2007, 7:05:41 PM1/14/07
to
Simon Richard Clarkstone <s.r.cla...@durham.ac.uk> writes:

> rincewind wrote:
> > I'm struggling with monads, and my brain hurts...
> > How somebody show me how I can implement an in-place modification of
> > an Array with monads? Say, I have to write the swap function (I'm
> > not sure about the correctness of this function declaration, but you
> > should get the idea):
> > swap ar i j :: Array a -> Ix -> Ix
>
> I have cross-posted this to comp.lang.haskell, which is for just this
> sort of thing.
>
> Try looking at Control.Monad.ST, Data.Array.ST, and Data.Array.MArray.
>
> The typeclasses used there are complicated, but your signature seems
> wrong to me.

The type "Array" is immutable; you will not in-place modify its elements.

The mutable arrays are IOArray and STArray; they require the IO monad
and the ST monad, respectively. In the ST case, as noted, the
relevant libraries are Control.Monad.ST and Data.Array.ST.
(Data.Array.MArray describes the generalization that unifies IOArray
and STArray. Therefore it will look abstract.)

In the ST case the correct type for swap should be:

swap :: (Ix i) => STArray s i e -> i -> i -> ST s ()

To swap, you read and write array elements. These are the functions
to use --- I show their types specialized to STArray (they have more
abstract types in Data.Array.MArray):

readArray :: (Ix i) => STArray s i e -> i -> ST s e
writeArray :: (Ix i) => STArray s i e -> i -> e -> ST s ()

Perhaps you are more interested in the IO monad at the moment. In the
IO case the types become:

swap :: (Ix i) => IOArray i e -> i -> i -> IO ()

readArray :: (Ix i) => IOArray i e -> i -> IO e
writeArray :: (Ix i) => IOArray i e -> i -> e -> IO ()

I'm wondering whether I should stop now and let you finish, now that
the objective and the tools are more concrete. But what the heck, if
you have problem even with monads...

swap ar i j = do x <- readArray ar i
y <- readArray ar j
writeArray arr i y
writeArray arr j x

I hope with the concrete types I've given, you can see what's going
on. (I practice Type-Oriented Programming.)

Simon Richard Clarkstone

unread,
Jan 15, 2007, 7:26:54 PM1/15/07
to
Albert Lai wrote:
> I hope with the concrete types I've given, you can see what's going
> on. (I practice Type-Oriented Programming.)

Ah, that is great fun. You spend the first half of your time fiddling
around with types to try to get a suitably elegant arrangement of the
information, ensuring that the necessary info is being passed to the
right place and nothing needs to be supplied before it is known. Then
the rest of the program almost writes itself, and the types serve as
half the documentation.

This is one of the lesser-quoted advantages of static typing in
experimental programming. (I hasten to add that dynamic typing has
advantages over static typing too, lest I start a flamewar.)

Reply all
Reply to author
Forward
0 new messages