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

/ Cookbook.ListRecipeFiltering

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

Filtering Lists

Problem

You have a list with a bunch of items, but you want to have a copy of that list with some items removed.

Solution

filter, in PLT's standard library, allows you to filter a list based on a predicate.
> (require (lib "list.ss"))
> (filter odd? '(1 2 3 4 5 6 7 8 9 0))
(1 3 5 7 9)
> (filter string? '(14 "abc" cons "xyz" 23))
("abc" "xyz")

You can supply your own predicate, as well. Here we filter a list of numbers that are greater than some limit we've defined:

> (let ((limit 12))
    (filter (lambda (x) (> x limit)) (map (lambda (n) (* n n)) '(1 2 3 4 5 6 7 8))))
(16 25 36 49 64)

Discussion

PLT ships with a version of filter in the list.ss library. You can also use SRFI-1 for another version of the same function.

There are a few twists on this problem. SRFI-1's partition works like filter, except that it returns 2 lists, one with items that pass the filter, and the other with the rest of the items in the original list, e.g.

> (require (lib "1.ss" "srfi"))
> (filter string? '(14 "abc" cons "xyz" 23))
("abc" "xyz")
> (partition string? '(14 "abc" cons "xyz" 23))
("abc" "xyz")
(14 cons 23)

Also in SRFI-1, remove is the reverse of filter, returning a new list without the items satisfying the predicate supplied. In addition, there are ! versions of all these functions to modify a list in-place.

Also, SRFI-1's filter-map is another twist on filter, combining it with map. It's like a map that returns only the true values. Here we return the products of two lists, but only if both members of the lists are numbers:

> (filter-map (lambda (x y) (and (number? x) (number? y) (* x y))) '(a 1 b 3 c 7) '(cons 14 "abc"  "xyz" 23 9))
(14 63)


Comments about this recipe

Contributors

-- NoelWelsh - 02 Jun 2004

-- GordonWeakliem - 06 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: ListRecipes
TopicOrder: 999

 
 
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