Re: [Haskell-cafe] How to define a Monad instance

2012-07-31 Thread Thiago Negri
Thanks for the reply Ryan. That's exactly the type of thing I was trying to do: use the syntactical sugar of do-notation to express some replacement rules. Why am I doing this? A long time ago, when I was learning C, I did a small project (spaghetti code) to encrypt text files in some

Re: [Haskell-cafe] How to define a Monad instance

2012-07-30 Thread Ryan Ingram
To take this a step further, if what you really want is the syntax sugar for do-notation (and I understand that, I love sweet, sweet syntactical sugar), you are probably implementing a Writer monad over some monoid. Here's two data structures that can encode this type; data Replacer1 k a =

Re: [Haskell-cafe] How to define a Monad instance

2012-07-30 Thread Ryan Ingram
A couple typos: instance Monad Replacer1 where - instance Monad (Replacer1 k) where instance Monad Replacer2 k where - instance Monad (Replacer2 k) where I haven't tested any of this code, so you may have to fix some minor type errors. On Mon, Jul 30, 2012 at 10:38 PM, Ryan Ingram

Re: [Haskell-cafe] How to define a Monad instance

2012-07-29 Thread Johan Holmquist
As for understanding monads, you can try to define the State monad [1]. Not sure if it's the best example but it's intuitive in that it let's you thread a state behind the scenes. *** Not related to your question -- in your example if you want to translate characters but do not plan to change

[Haskell-cafe] How to define a Monad instance

2012-07-28 Thread Thiago Negri
Hello. I'm trying to understand Monads. In order to do so, I decided to create my own Monad for a simple domain-specific language. The idea is to define a way to describe a multi-value replacement inside do-notation. Example of a function doing what I want (without Monads): replaceAll :: (a -

Re: [Haskell-cafe] How to define a Monad instance

2012-07-28 Thread Steffen Schuldenzucker
On 07/28/2012 03:35 PM, Thiago Negri wrote: [...] As Monads are used for sequencing, first thing I did was to define the following data type: data TableDefinition a = Match a a (TableDefinition a) | Restart So TableDefinition a is like [(a, a)]. [...] So, to create a replacement table: