Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to use monads?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
rincewind  
View profile  
 More options Jan 14 2007, 8:51 am
Newsgroups: comp.lang.functional
From: rincewind <f...@not.real>
Date: Sun, 14 Jan 2007 16:51:34 +0300
Local: Sun, Jan 14 2007 8:51 am
Subject: [Haskell] How to use monads?
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Richard Clarkstone  
View profile  
 More options Jan 14 2007, 12:23 pm
Newsgroups: comp.lang.functional
From: Simon Richard Clarkstone <s.r.clarkst...@durham.ac.uk>
Date: Sun, 14 Jan 2007 17:23:01 +0000
Local: Sun, Jan 14 2007 12:23 pm
Subject: Re: [Haskell] How to use monads?

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.

--
Simon Richard Clarkstone: s.r.cl?rkst...@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/)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Richard Clarkstone  
View profile  
 More options Jan 14 2007, 12:24 pm
Newsgroups: comp.lang.functional, comp.lang.haskell
From: Simon Richard Clarkstone <s.r.clarkst...@durham.ac.uk>
Date: Sun, 14 Jan 2007 17:24:30 +0000
Local: Sun, Jan 14 2007 12:24 pm
Subject: Re: [Haskell] How to use monads?

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.

(I hope my cancel message worked.)

--
Simon Richard Clarkstone: s.r.cl?rkst...@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/)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert Lai  
View profile  
 More options Jan 14 2007, 7:05 pm
Newsgroups: comp.lang.functional, comp.lang.haskell
From: Albert Lai <tre...@shell.vex.net>
Date: 14 Jan 2007 19:05:41 -0500
Local: Sun, Jan 14 2007 7:05 pm
Subject: Re: [Haskell] How to use monads?
Simon Richard Clarkstone <s.r.clarkst...@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.)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Type-Oriented Programming (was: Re: [Haskell] How to use monads?)" by Simon Richard Clarkstone
Simon Richard Clarkstone  
View profile  
 More options Jan 15 2007, 7:26 pm
Newsgroups: comp.lang.functional, comp.lang.haskell
From: Simon Richard Clarkstone <s.r.clarkst...@durham.ac.uk>
Date: Tue, 16 Jan 2007 00:26:54 +0000
Local: Mon, Jan 15 2007 7:26 pm
Subject: Type-Oriented Programming (was: Re: [Haskell] How to use monads?)

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.)

--
Simon Richard Clarkstone: s.r.cl?rkst...@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/)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google