[Haskell-cafe] Really confused

Mark Carter mcturra2000 at yahoo.co.uk
Wed Sep 21 14:12:54 EDT 2005



I'm trying to define a type called Stream. For now, assume that that the 
type has
3 fields: uid, x, y. X and y represent a point in space that a stream 
occupies, and
uid is a unique identifier for the stream. The uid should be 
"auto-generated". It's
important that streams have an identity so that they can be referred to 
and manipulated.
Streams will
be put together into a list, and they'll eventually need to be able to 
point to one
another. It would be nice to be able to print the  uid, just to show that it
works.

Now it occured to me that what one might want to generate the uids is a 
monad, let's
call it UID. It'll have a function get, which returns another 
identifier. I assumed that
the best solution for the problem would be in terms of monads, because 
successive
calls to get return different results; i.e. there's a bit of state going 
on inside. The
values returned can start from 1, and increment by 1 each time.

I had a look at some documentation at:
http://www.haskell.org/tutorial/monads.html
and I'm afraid my brain just froze.

I get the idea that
data SM a = SM (S -> (a,S))
maps a state to a result, and a new state. OTOH, looking at

instance Monad SM where
  -- defines state propagation
  SM c1 >>= fc2         =  SM (\s0 -> let (r,s1) = c1 s0
                                          SM c2 = fc2 r in
                                         c2 s1)
  return k              =  SM (\s -> (k,s))

just confuses me no end.

Any pointers, like am I taking completely the wrong approach anyway? I'm 
also puzzled
as to how the initial state would be set.

	
	
		
___________________________________________________________ 
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com


More information about the Haskell-Cafe mailing list