
Sorry the newbie question and the insistence, but how exactly do I use map or fold applied to the infinite list to get successive integers at each time? Thank you

On 2003-10-12 at 21:23BST Jose Morais wrote:
Sorry the newbie question and the insistence, but how exactly do I use map or fold applied to the infinite list to get successive integers at each time?
You don't. The infinite list /is/ all the successive integers. I think at this point it would be useful if you could say what you are trying to do -- what programme are you trying to write, as opposed to what imperative-sounding programming technique you are trying to duplicate. For example, where in an imperative language you might print the first 10 squares by setting a variable to successive integers, in Haskell you could write: positive_naturals = [1..] squares = map (^2) positive_naturals main = print $ take 10 squares which has a similar effect Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk

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

Jose Morais wrote:
Sorry the newbie question and the insistence, but how exactly do I use map or fold applied to the infinite list to get successive integers at each time?
I think it would be best if you gave us a description of what you're trying to accomplish. When programming in a pure language, it is often necessary to turn one's thinking `inside out' from the approach that would be used in a language with assignment. HTH, --ag -- Artie Gold -- Austin, Texas Oh, for the good old days of regular old SPAM.
participants (4)
-
Artie Gold
-
Jerzy Karczmarczuk
-
Jon Fairbairn
-
Jose Morais