Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Teach Yourself Scheme: 6.4 Mapping a procedure across a list

created by washort

(idea) by washort (6.8 y) (print)   ?   (I like it!) Tue Apr 18 2000 at 16:34:26

Prev Up Next

A special kind of iteration involves repeating the same action for each element of a list. Scheme offers two procedures for this situation: map and for-each.

The map procedure applies a given procedure to every element of a given list, and returns the list of the results. Eg,

(map add2 '(1 2 3))
=> (3 4 5)

The for-each procedure also applies a procedure to each element in a list, but returns void. The procedure application is done purely for any side-effects it may cause. Eg,

(for-each display
  (list "one " "two " "buckle my shoe"))

has the side-effect of displaying the strings (in the order they appear) on the console.

The procedures applied by map and for-each need not be one-argument procedures. For example, given an n-argument procedure, map takes n lists and applies the procedure to every set of n of arguments selected from across the lists. Eg,

(map cons '(1 2 3) '(10 20 30))
=> ((1 . 10) (2 . 20) (3 . 30))

(map + '(1 2 3) '(10 20 30))
=> (11 22 33)

Prev Up Next


printable version
chaos

Teach Yourself Scheme: 7 I/O Teach Yourself Scheme Barbara Lee's statement against HR 64 open source
Teach Yourself Scheme: 6.3 Iteration How Pac-Man got his name cache MP3 ID3v1 Tag genre mapping
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Look at this mess the Death Borg made!
Sea dragon
A Computer Prayer
Common Glasgow, Edinburgh and Scottish colloquial terms and words
People Power
Harlan Ellison
Music For Supermarkets
Methamphetamine
Enigma
If God did not exist, it would be necessary to invent him
I DON'T quit
Care instructions for the Juniper Bonsai
Don Quixote
Also Sprach Zarathustra
New Writeups
XWiz
Trism(review)
artman2003
Briefcase Full of Souls - Part I(fiction)
Dreamvirus
Alan Ladd(person)
waverider37
Harold Holt(person)
The Debutante
Until death do us part(fiction)
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
This page courtesy of The Everything Development Company