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

/ ListRecipes / Cookbook.ListRecipeListMinus

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

Finding Elements in One Array but Not Another

Problem

You want to find elements that are in one list but not another.

Solution

You want to find elements in al list that aren't in bl list. Build a hash of the keys of bl to use as a lookup table. Then check each element in al to see whether it is in bl.

(require (lib "1.ss" "srfi"))

;; To find elements that are in the al list but not in the bl list
(define (list-minus al bl)
  (let ((h (make-hash-table 'equal)))
    (for-each (lambda (v)
                (hash-table-put! h v #f))
              bl)
    (filter (lambda (v) 
              (hash-table-get h v (lambda () #t)))
            al)))

Discussion

Another way is to use the lset-difference function shipped in the SRFI 1, but this function does not use hashes at all, and therefore it is slower.


Comments about this recipe

Contributors

-- IvanHernandez - 19 May 2004

CookbookForm
TopicType: Recipe
ParentTopic:
Next Topic:

 
 
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