Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-beginners] Global variable question

4 views
Skip to first unread message

Joe Van Dyk

unread,
Dec 30, 2009, 8:49:44 PM12/30/09
to begi...@haskell.org
What's the appropriate way to write this in Haskell?

# Ruby code, ignore the thread-unsafety for now.
class Ticker
@@tick = 0
def self.update!
@@tick += 1
end
def self.current
return @@tick
end
end

Ticker.update! # 1
Ticker.update! # 2
Ticker.current # 2


--
Joe Van Dyk
http://fixieconsulting.com
_______________________________________________
Beginners mailing list
Begi...@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

Brent Yorgey

unread,
Dec 30, 2009, 9:00:03 PM12/30/09
to begi...@haskell.org
On Wed, Dec 30, 2009 at 05:49:12PM -0800, Joe Van Dyk wrote:
> What's the appropriate way to write this in Haskell?
>
> # Ruby code, ignore the thread-unsafety for now.
> class Ticker
> @@tick = 0
> def self.update!
> @@tick += 1
> end
> def self.current
> return @@tick
> end
> end
>
> Ticker.update! # 1
> Ticker.update! # 2
> Ticker.current # 2

If you really want a global ticker, use the State monad:

tick :: State Int ()
tick = modify (+1)

-- increment the counter twice then return its new value
mainStuff :: State Int Int
mainStuff = tick >> tick >> get

-Brent

Edward Z. Yang

unread,
Dec 30, 2009, 9:06:01 PM12/30/09
to Joe Van Dyk, beginners
Excerpts from Joe Van Dyk's message of Wed Dec 30 20:49:12 -0500 2009:

> What's the appropriate way to write this in Haskell?

Hi Joe,

The Ticker class is an example of stateful computation, which we try
to avoid in Haskell. What are you really trying to do?

* If you a number, and you want to add to it, (+1) works
plenty well.

* If you are going to be making ticks, but you don't care about
reading the ticks until the end of the computation, the Writer monad
is of interest.

* If you need to update the tick count and read it, the State
monad is a good bet.

* If you're interested in curiosities from imperative land, STRef, IORef and
other similar creatures may be helpful.

Cheers,
Edward

John Velman

unread,
Dec 31, 2009, 6:38:12 PM12/31/09
to begi...@haskell.org
On Thu, Dec 31, 2009 at 09:37:10AM -0800, Joe Van Dyk wrote:

> On Wed, Dec 30, 2009 at 6:05 PM, Edward Z. Yang <ezy...@mit.edu> wrote:
> > Excerpts from Joe Van Dyk's message of Wed Dec 30 20:49:12 -0500 2009:
> >> What's the appropriate way to write this in Haskell?
> >
> > Hi Joe,
> >
> > The Ticker class is an example of stateful computation, which we try
> > to avoid in Haskell. �What are you really trying to do?
> >
> > * If you a number, and you want to add to it, (+1) works
> > �plenty well.
> >
> > * If you are going to be making ticks, but you don't care about
> > �reading the ticks until the end of the computation, the Writer monad
> > �is of interest.
> >
> > * If you need to update the tick count and read it, the State
> > �monad is a good bet.
> >
> > * If you're interested in curiosities from imperative land, STRef, IORef and
> > �other similar creatures may be helpful.
>
> It's like haskell is on a personal vendetta against me to make me feel dumb.


It's not personal.

John Velman.


>
>
> --
> Joe Van Dyk
> http://fixieconsulting.com

Joe Van Dyk

unread,
Dec 31, 2009, 6:44:04 PM12/31/09
to John Velman, begi...@haskell.org
On Thu, Dec 31, 2009 at 3:38 PM, John Velman <vel...@cox.net> wrote:
> On Thu, Dec 31, 2009 at 09:37:10AM -0800, Joe Van Dyk wrote:
>> On Wed, Dec 30, 2009 at 6:05 PM, Edward Z. Yang <ezy...@mit.edu> wrote:
>> > Excerpts from Joe Van Dyk's message of Wed Dec 30 20:49:12 -0500 2009:
>> >> What's the appropriate way to write this in Haskell?
>> >
>> > Hi Joe,
>> >
>> > The Ticker class is an example of stateful computation, which we try
>> > to avoid in Haskell.  What are you really trying to do?
>> >
>> > * If you a number, and you want to add to it, (+1) works
>> >  plenty well.
>> >
>> > * If you are going to be making ticks, but you don't care about
>> >  reading the ticks until the end of the computation, the Writer monad
>> >  is of interest.
>> >
>> > * If you need to update the tick count and read it, the State
>> >  monad is a good bet.
>> >
>> > * If you're interested in curiosities from imperative land, STRef, IORef and
>> >  other similar creatures may be helpful.
>>
>> It's like haskell is on a personal vendetta against me to make me feel dumb.
>
>
> It's not personal.

Feels like it though. :(

0 new messages