monads

Haskell: monadic takeWhile?

I have some functions written in C that I call from Haskell. These functions return IO (CInt). Sometimes I want to run all of the functions regardless of what any of them return, and this is easy. For sake of example code, this is the general idea of what's happening currently: Prelude> let f x = print x >> return x Prelude> mapM_ f ...

Haskell: Can I use a where clause after a block with bind operators (>>=)?

I have a very simple question. I'd like to use a where clause after a bloc of code that uses bind operators but I get a compilation error. Here is a simple example: main = putStrLn "where clause test:" >> return [1..10] >>= \list -> print list' where list' = reverse list -- test1.hs:5:28: Not in scope: `list' I ca...

Values inside monads, nested in data structures?

Suppose that in a Haskell program I have some data whose type is something like: IO [ IO (Int, String, Int) ], or IO [ (Int, String, IO Int) ], or [ (Int, String, IO Int) ] but I have pure functions that should operate on [ (Int, String, Int) ]. It seems that I'd have to clumsily remove the inside values from the IO monad, until I g...

Evil use of Maybe monad and extension methods in C#?

I've been thinking about the null propagation problem in .NET, which often leads to ugly, repeated code like this: Attempt #1 usual code: string activeControlName = null; var activeForm = Form.ActiveForm; if (activeForm != null) { var activeControl = activeForm.ActiveControl; if(activeControl != null) { activeContro...

Simplifying some Haskell code

So I'm working on a minimax implementation for a checkers-like game to help myself learn Haskell better. The function I'm having trouble with takes a list for game states, and generates the list of immediate successor game states. Like checkers, if a jump is available, the player must take it. If there's more than one, the player can ...

Methods for side-effects in purely functional programming languages

At the moment I'm aware of the following methods to integrate side-effects into purely functional programming languages: effect systems continuations unique types monads Monads are often cited to be the most effective and most general way to do this. Which other methods exist? How do they compare? ...

Why doesn't a python dict.update() return the object?

I 'm trying to do : award_dict = { "url" : "http://facebook.com", "imageurl" : "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count" : 1, } def award(name, count, points, desc_string, my_size, parent) : if my_size > count : a = { "name" : name, "description" : desc_st...

In C# adding SelectMany extends linq to a new monad type, how do I do the same thing in VB.net?

An old Yet Another Language Geek blog post explaining monads describes adding a SelectMany extension method to C# in order to extend the linq syntax to new types. I've tried it in C# and it works. I did a straight conversion to VB.net and it doesn't work. Does anyone know if VB.net supports this feature or how to use it? Here is the C#...

looking for learning exercise: implement these monads

When learning about new programming subjects I usually follow a pattern: I read about it, I understand it, and then I code up a few examples to make sure I really get it. I've read a lot about monads, and I'm confident that I understand and get them. I'm now at a stage where I'd really like to code up a few monads to solidify my under...

Some questions about monads in Haskell

I'm learning about monads and have a few questions. This is where I am right now. Please correct me where I am wrong. The >>= symbol is an infix operator. Infix operators are functions that take two arguments (left-hand side and right-hand side) and return a value. The >>= symbol is called the bind operator and has signature Monad m =...

pipe readers and writers

why cant we have multiple writers in pipe but single reader ? ...

A Haskell function of type: IO String-> String

I wrote a bunch of code in Haskell to create an index of a text. The top function looks like this: index :: String -> [(String, [Integer])] index a = [...] Now I want to give this function a String read from a file: index readFile "input.txt" Which won't work because readFile is of type FilePath -> IO String. Couldn't match exp...

Why there is no something like IMonad<T> in upcoming .NET 4.0

... with all those new (and not so new if we count IEnumerable) monad-related stuff? interface IMonad<T> { SelectMany/Bind(); Return/Unit(); } That would allow to write functions that operate on any monadic type. Or it's not so critical? ...

Creating monads in haskell

I want to create my own monad. This is what i wrote: data LeafConType a = LeafCon (a,Int,Int) instance Monad (LeafConType ) where return = LeafCon lc@(LeafCon (t,i,n)) >>= f = if i>=n then lc else f (t,i,n) But this dont work. Ghc says: leafcon.hs:26:1: Occurs chec...

Can somebody walk me through this Haskell function (State monad related)?

tick :: State Int Int tick = get >>= \n -> put (n+1) >>= \y -> return n I'm confused as to how put (n+1) has any effect on the end result of this function at all. It seems like this function should return the initial state unchanged. I'm trying to run through this in my mind, but I keep running out of room to hold things ...

implementing a per-digit counter using the list monad

So, I was looking at the question here, and built a rather ugly solution for the problem. While trying to clean it up, I started investigating list comprehensions and the list monad. What I decided to do was to implement a per-digit counter using the list monad. Given an input sequence of digits, [1, 2], I wanted to generate an output...

When is it OK to use an IORef?

One thing that has always confused me is whether or not it's an okay time to use an IORef. Are there any guidelines that should be followed when deciding whether or not to use an IORef for a task? When is a good time to use the State monad over an IORef? ...

State Monad, sequences of random numbers and monadic code

I'm trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Generator (probably not good, but my intention is just to learn the State Monad, not build a good RNG library). The generator is just this (I want to generate a sequence...

What programming task provided your breakthrough with monads?

In a recent blog post about a probability monad he'd written, Mark Dominus wrote, "So I feel like I've finally arrived, monadwise." My first monadic program was an awkward solution to Problem 32 from Project Euler using parsec and the Maybe monad. What were you working on when the light finally turned on for you? Provide at least a ske...

Why Option[T] ?

Hi everyone, I am a beginner to functional programming and I have recently started studying Scala and I really love this language for the all goodies it provides like closures, pattern matching, currying etc. However I am not able to understand the point of Option[T] class in Scala. I mean, I am not able to see any advanages of None ov...