global variable

Jon Fairbairn Jon.Fairbairn at cl.cam.ac.uk
Sun Oct 12 21:45:30 EDT 2003


On 2003-10-12 at 20:20BST Jose Morais wrote:
> Hello,
> 
> 	I need a function called, say, newItem, that when
> first called returns 1, the next time it is called it
> would return 2, then 3 and so on. How can I achieve this?

You can't (as Glynn has explained), and most of the time
it's the wrong thing to do anyway. Apart from the monadic
approach (which is perhaps a bit heavyweight), another way
of tackling this sort of thing is to used infinite lists.

item_numbers:: [Integer]
item_numbers = [1..]

You can then operate on those with map and fold.

 Jón

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-Cafe mailing list