Although the LIST function can be use to combine two of more lists into a single list, this function has one major disadvantage for using it in this capacity. Each list that is supplied to the function becomes elements of the newly created list. For example, using the LIST function to combine the lists (5.5 4.3 2.3) and (44.7 39.6 12.4) would yield the list ((5.5 4.3 2.3) (44.7 39.6 12.4)). This is illustrated in the following example.
(SETQ list1 (LIST 5.5 4.3 2.3)) ;Creates a new list setting it to the variable ;list1.
(5.5 4.3 2.3) ;Result returned from the previous ;expression.
(SETQ list2 (LIST 44.7 39.6 12.4)) ;Creates a new list setting it to the variable ;list2.
(44.7 39.6 12.4) ;Result returned from the previous ;expression.
(SETQ list3 (LIST list1 list2)) ;Combines list1 and list2 into a single list ;setting it to the variable list3.
((5.5 4.3 2.3) (44.7 39.6 12.4)) ;Result returned from the previous ;expression.
As illustrated in the above example, the lists themselves become entities that are considered as single elements in the newly formed list. In some cases this result may be desirable. In those instances where this result is not acceptable, another method must be employed. In cases where the elements of the individual list are needed to create a new list, then the APPEND function, must be used. The APPEND function (APPEND list …), extracts the elements from each individual list supplied and returns a single list. For example, by supplying the lists from the previous example as arguments to the APPEND function the result (5.5 4.3 2.3 44.7 39.6 12.4) would be returned. This is illustrated in the following example.
(SETQ list1 (LIST 5.5 4.3 2.3)) ;Creates a new list setting it to the variable ;list1.
(5.5 4.3 2.3) ;Result returned from the previous ;expression.
(SETQ list2 (LIST 44.7 39.6 12.4)) ;Creates a new list setting it to the variable ;list2.
(44.7 39.6 12.4) ;Result returned from the previous ;expression.
(SETQ list3 (APPEND list1 list2)) ;Combines both list1 and list2 into a single ;list setting it to the variable list3.
(5.5 4.3 2.3 44.7 39.6 12.4) ;Result returned from the previous ;expression.
No comments:
Post a Comment