
Nosso amigo José Moreira perguntou:
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?
Several people, among whom Jón Fairbarn explained what are the problems with that in Haskell. The standard proposal to 'generate' a, then b,c,d,e ... is to make a lazy list [a,b,c,d,e,... ] and to iterate over it. Since José seems not to master the notion of purely functional processing of such 'non-deterministic collections', the magical advice to use monads, seeds, etc. seem less useful, though... My answer was triggered by the somehow intransigent formulation of Jón:
You can't (as Glynn has explained), and most of the time it's the wrong thing to do anyway.
== Saying that something is a "wrong thing to do" is too normative, Jón. I believe that even on the Haskell list we might try to understand the *needs* of a potential Haskell user or a non-user. 1. If José wants to keep intact his idea of multiple calls giving different results, then the True Functionalists should convince him that there are other nice languages. The notion of *generators* whose purpose is EXACTLY this, is a popular one, and there is nothing 'wrong' with it. * Use Python, and, the "yield" construct, and 'next'. * Use streams and 'next' (or 'do') in Smalltalk. * Use Icon and its generators. Very well explained. * Try to master the relational programming, backtracking, and use Prolog. * Manufacture your generators using Object-Oriented programming, as the Smalltalkers did. Instead of using a global 'seed', embed all the state in an object and define the 'next' method. 2. I agree with all my venerable predecessors that José should perhaps specify his "real needs", then the Only True Functional Church might show him the way of Salvation, by helping him to design his algorithms in a functional way. We are convinced that we might help you, José, but there is a price: you will have to understand what the 'laziness' is, or perhaps what is a 'continuation'. [[I claim, however, that nobody will oblige you to learn what a 'monad' is, although it might do some good for you one day.]] Jerzy Karczmarczuk Caen, France