Thursday, February 18, 2010

Retrieving Information from a List

As mentioned earlier, the individual entries in a list are known as Elements. An element can either be a string, a real number, an integer, another list, an expression (single quoted), or any combination. So how are the elements of a list extracted? By either using one or a combination of two or more of the five basic list retrieval functions. These functions are CAR, CDR, CADR, CADDR and NTH. Although, the syntax for the CXXXX functions is the same, (CXXXX list), their output are different. The CAR function returns the first element of a list, while the CDR function creates a list starting with the second element. For example, supplying the list (4.002 3.45 5.7) to both the CAR and CDR functions return the following results.

(SETQ list_example (LIST 4.002 3.45    5.7))

; Creates a list, and sets it to the variable

; list_example.

(CAR list_example)    ;Returns the first element of the list.

4.002                            ;Returned value.

(CDR list_example)    ;Creates a new list starting with the second ;element.

(3.45 5.7)    ;New list created from the CDR function.

The CADR function combines the capabilities of the CAR and CDR functions. It returns only the second element of a supplied list. To illustrate this, the following example uses the list created in the previous example to compare the results returned by the CADR function and an expression containing both the CAR and CDR functions.

(CAR (CDR list_example))    ; The CDR function creates a new list

; starting with the second element and

; returning that list to the CAR function.

; The CAR function then returns the first

; element of the list supplied by the CDR

; function.

3.45    ; Returned value.

(CADR list_example)    ; The CADR function returns only the

; second element of the supplied list.

3.45    ; Returned value.

Although both the CADR and the combination CAR/CDR functions return the same value, the CADR function provides a shorter route to obtaining the same results. If only the second element of a list is required, then the CADR function not only saves valuable time and the possibility of making a mistake, it could also reduce the overall size of the program, resulting in a program that loads and runs faster.

The last of the CXXXX functions is the CADDR function. This function returns the third element of a list. It could be compared to using the CDR function twice, followed by the CAR function or the CDR function followed by the CADR function. Again, the advantage of using this function in place of any of the combinations reduces the possibility of making a mistake, as well as shortens the length of the program. This is illustrated in the following example, where both the CADDR function and the two combinations are supplied with the list from the previous two examples.

(CADDR list_example)    ;The CADDR function returns only the third element ; of a list.

5.7    ; Returned value.

(CADR (CDR list_example))    ;The CDR function creates a new list starting with

; the second element and returns that list to the

; CADR function. The CADR function then returns

; the second element of the list supplied by the CDR

; function.

5.7    ; Returned value.

(CAR (CDR (CDR list_example)))     ;The first CDR function creates a new list starting

; with the second element and returns that list to the

; next CDR function in the expression. This CDR

; function then creates another list starting from the

; second element supplied to it from the previous

; CDR function. The list is then passed on to the

; CAR function where the first element is returned.

5.7    ; Returned value.

An alternative to using the CXXXX functions is the NTH function (NTH number list). This function, when supplied with a list, returns the element specified by the programmer. The programmer specifies which element is to be returned by entering the integer value representing the position of the element in the supplied list. The elements are numbered starting with zero and continuing in the positive direction. For example using the list provided in the previous example, the third element is extracted as follows.

(NTH 2 list_example)                ;The NTH function returns an elements specified by

; the programmer. In this example the programmer

; has specified the third element. The first element is

; considered to be located at the zero position while

; the third element is considered to be at the second

; position.

5.7                        ; Returned value.

No comments:

Post a Comment