Switch the screen on/off Go back to last page Go forward one page Find out more details about the advertisement
John Moores University. Main CWIS Site
  

Back to List of Functions 

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 

[3, 4, 5, 6] 

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:

fee(z) := 
    Prog 
      x := [] 
      n := 1 
      Loop 
        If n > 5 
          RETURN x 
      x := ADJOIN([n], x)
      n :+ 1 


simplifies to 

Which is a column vector. So:

fee(z) := 
    Prog 
       x := [] 
       n := 1 
       Loop 
        If n > 5 
        RETURN x 
       x := APPEND(x, [n])
       n :+ 1 

simplifies to 

[1, 2, 3, 4, 5]

Which is a row vector!

We can modify this sequence slightly, to produce a Fibonacci sequence:

fib(z, x, n) := 
    Prog 
       x := [1] 
       n := 1 
       Loop 
        If n > z 
        RETURN x 
       x := APPEND(x, [n])
       n :+ x
¯(DIM(x) - 1)

So,

fib(200)=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

It is possible to inititialise variables in the argument list, hence simplifying the code:
fib(z, x:=[1], n:=1) := 
    Prog 
       Loop 
         If n > z 
           RETURN x 
         x := APPEND(x, [n])
         n :+ x
¯(DIM(x) - 1)

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.,
fib(z, x:=[1], n:=1) := 
    Prog 
       Loop 
         If n > z 
           RETURN REVERSE(x) 
         x := ADJOIN(n, x)
         n :+ FIRST(REST(x))

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()...