• Aucun résultat trouvé

Some Handy Procedures that Operate on Lists

Scheme provides a variety of procedures for operating on lists, so that you usually don't have to think about pairs--you can think about whole lists. I'll discuss these procedures in more detail later [ put in link ], but here's a brief introduction.

None of these procedures modifies its arguments--they may take lists as arguments, but they return new lists without modifying the old ones.

Go to the first, previous, next, last section, table of contents.

http://www.federated.com/~jim/schintro-v14/schintro_40.html11/3/2006 8:58:25 PM

An Introduction to Scheme and its Implementation - length

Go to the first, previous, next, last section, table of contents.

length

length takes one argument, a list, and returns an integer giving the length of the list. For example, (length '(0 #t #f)) returns 3.

Go to the first, previous, next, last section, table of contents.

http://www.federated.com/~jim/schintro-v14/schintro_41.html11/3/2006 8:58:30 PM

An Introduction to Scheme and its Implementation - list

Go to the first, previous, next, last section, table of contents.

list

list takes one or more arguments and constructs a list of those items. That is, a cdr-linked, null-terminated sequence of pairs is created, where each pair's car fields holds one of the values passed to list.

Notice that this is different from cons, in that the arguments are not lists in general--they're just any items that should be put into a list.

Intuitively, we often use cons to push one item onto a list that already exists, but we use list to create a list from scratch.

Notice that if we hand list just one argument, e.g., (list 1), that creates one pair whose cdr is null and whose car is the given argument. In contrast, if we use cons to create a one-element list, we must pass it that element and an empty list to serve as the cdr value: (cons 1 '()).

Go to the first, previous, next, last section, table of contents.

http://www.federated.com/~jim/schintro-v14/schintro_42.html11/3/2006 8:58:36 PM

An Introduction to Scheme and its Implementation - append

Go to the first, previous, next, last section, table of contents.

append

append takes two or more lists and constructs a new list with all of their elements. For example, (append '(1 2) '(3 4))

returns a list (1 2 3 4).

Notice that this is different from what list does:

(list '(1 2) '(3 4))

returns ((1 2) (3 4)), a two element list of the lists it was given. list makes its arguments elements of the new list, independent of whether the arguments are lists or something else.

append requires that its arguments are lists, and makes a list whose elements are the elements of those lists--in this case, a four-element list. Intuitively, it concatenates the lists it is given. It only concatenates the top-level structure, however--it doesn't "flatten" nested structures. For example

(append '((1 2) (3 4)) '((5 6) (7 8)))

returns ((1 2) (3 4) (5 6) (7 8))

append doesn't modify any of its arguments, but the result of append generally shares structure with the last list it's given. (It effectively conses the elements of the other lists onto the last list to create the result list.) It's therefore dangerous to make a "new" list with append and then modify the "old" list. This is one of the reasons side effects are discouraged in Scheme.

Go to the first, previous, next, last section, table of contents.

http://www.federated.com/~jim/schintro-v14/schintro_43.html11/3/2006 8:58:41 PM

An Introduction to Scheme and its Implementation - reverse

Go to the first, previous, next, last section, table of contents.

reverse

reverse takes one list, and returns a new list with the same elements in the opposite order. For example,

(reverse '(1 2 3 4))

returns a list (4 3 2 1). Like append only reverses the top-level structure of the list it's given.

(reverse '((1 2) (3 4)))

returns ((3 4) (1 2)), not ((4 3) (2 1))

Go to the first, previous, next, last section, table of contents.

http://www.federated.com/~jim/schintro-v14/schintro_44.html11/3/2006 8:58:47 PM

An Introduction to Scheme and its Implementation - member

Go to the first, previous, next, last section, table of contents.

member

member takes any value and a list, and searches the list for that value. If it finds it, it returns a pointer to the first pair whose car holds that value, i.e., the "rest" of the list starting at the point where the searched-for item was found. If it is not found, #f is returned. (The return value is theresearched-fore always either a pair or the false object.)

(member 22 '(18 22 #f 300)) returns (22 #f 300).

Notice that member can be used either to find a value's location in a list, or as a predicate to check whether the item is in the list at all. Since pairs are true values, you can use the result of member in a conditional expression and it will count as true if the item is found.

[ Maybe I should introduce strings and symbols here, moving some material from the tutorial chapter here and possibly expanding the tutorial with more examples. ]

Go to the first, previous, next, last section, table of contents.

http://www.federated.com/~jim/schintro-v14/schintro_45.html11/3/2006 8:58:52 PM

An Introduction to Scheme and its Implementation - Recursion Over Data Structures

Go to the first, previous, next, last section, table of contents.