6.7 Use REVERSE to write a NEXT-TO-LAST function that returns the Write another version using NTH and LENGTH. Write another version of LAST-ELEMENT using REVERSE instead of LAST. 6.6 Use the LAST function to write a function called LAST-ELEMENT that returns the last element of a list instead of the last cons cell. ( append line '(violets are blue)) => (ROSES ARE RED VIOLETS ARE BLUE) ( cons (last line) line) => ((RED) ROSES ARE RED) ( list (first line) (last line)) => (ROSES (RED)) ( append (last line) line) => (RED ROSES ARE RED) ( append line ( list (first line))) => (ROSES ARE RED ROSES) (reverse (reverse line)) => ROSES ARE RED Then write down what each of the following expressions evaluates to: 6.5 Write an expression to set the global variable LINE to the list (ROSES ARE RED). ( setf spell '( a b r a c a d a b r a)) => (A B R A C A D A B R A) REMOVE builds its result out of fresh cons cells by copying (parts of) the list. It does not change any variables or cons cells when removing elements from a list. The result returned by REMOVE is a new list, without the deleted items. 6.4 What is the value of (LAST '((A B C))), and why? => ((A B C)) because is a list inside of a list, so returns that list. 6.3 What is the value of (LAST '(ROSEBUD))? => (ROSEBUD) (last 'nevermore) => Error! NEVERMORE is not a list. LAST returns the last cons cell of a list. D)), and why? Error, because D isn't a list. 6.2 What is the value of (NTH 3 '(A B C. 6.1 Why is (NTH 4 '(A B C)) equal to NIL? => because last position of the list is 2. NTH => takes the CAR of the NTHCDR of a list. NTHCDR => returns the nth successive cdr of a list. We can use REVERSE to add an element to the end of a list, as follows. It copies its input rather than modifying it. REVERSE => returns the reversal of a list.
0 Comments
Leave a Reply. |