| |||||||||||
|
Try a more detailed version of append
The following can show some subtle differences between append and adjoin. In a simple context, take APPEND([1, 2, 3], [4, 5, 6]) this simplifies to [1, 2, 3, 4, 5, 6] whereas ADJOIN(1, 4) simplifies to 14. Alternatively, ADJOIN(3, [4, 5, 6]) simplifies to In both cases, some or all of the arguments are vectors, denoted by the square [ ] brackets. In a programming context, suppose we start with an empty set, [ ], and add an increment:
Which is a column vector. So:
simplifies to Which is a row vector! We can modify this sequence slightly, to produce a Fibonacci sequence:
So, It is possible to inititialise variables in the argument list, hence simplifying
the code:
or in 1-D fib(z, x := [1], n := 1) := LOOP(IF(n > z, RETURN x), x := APPEND(x, [n]), n :+ x SUB (DIM(x) - 1)) Also,
this code adds elements to the back of a list. In general this is
not considered good practice in a LISP environment. So we add everything
to the front of the list and then reverse the list at the end. i.e.,
or in 1-D fib(z, x := [1], n := 1) := LOOP(IF(n > z, RETURN REVERSE(x)), x := ADJOIN(n, x), n :+ FIRST(REST(x))) FIRST() and REST() are LISP like functions, where FIRST(v) returns the first element of the vector v. REST(v) returns a vector of all but the first element of v. Notice that FIRST(REST(x)) will pick out the second element of the list x.
Plotting this, gives a kind of exponential curve. If we take natural logs of the above vector, guess what kind of plot we now have? To find out, and to explore a very useful Derive function MAP_LIST()...
| |||||||||||||||||||