s c h e m a t i c s : c o o k b o o k

/ Cookbook.IdiomStreams

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Streams

Problem

You want to handle state in a functional manner.

Solution

The concept you're looking for is called a "Stream".

Discussion

The canonical reference for Streams is in SICP, where Abelson and Sussman introduce the concept in Section 3.5. The motivation behind Streams is to model a list of potentially infinite size. Clearly, even if you take infinite to mean "a very big list", memory would be a problem. In the example from SICP, they consider the case of searching for large prime integers. It would be more schemely to use something like filter on a list of integers to return the list of prime integers for a given range, but for large integers, you would need to retain a very long list, because primes get farther apart the bigger the numbers in question get. The idea extends to any data structure that's bigger than memory, or to any operation that's expensive (either computationally or due to I/O costs) - delaying the evaluation of any expensive operation will improve performance, particularly if the results of evaluating that expression may not be used.

SRFI-40 defines a portable stream library. The primary difference between the SRFI and the SICP approaches is that SICP introduced an "odd" stream approach, while the SRFI follows an "even" approach. The SRFI document has an excellent comparison of the two approaches, but the simple explanation of the difference is that even streams delay evaluation of the stream until absolutely necessary, whereas odd streams are evaluated at the time of reference, and are therefore subject to being evaluated once more than necessary.


Comments about this recipe

Contributors

-- NoelWelsh - 20 Sep 2004

-- GordonWeakliem - 20 Sep 2004

CookbookForm
TopicType: RecipeStub
ParentTopic: IdiomRecipes
TopicOrder: 999

Attachment: Action: Size: Date: Who: Comment:
prime-stream.ss action 1029 14 Aug 2006 - 00:17 EricHanchrow example of using a stream of primes to factor numb

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest