Prolog - List Append.

LA home
Computing
Logic
 Prolog
  Intro.
  Syntax
  Examples
   Windsor
   Append
   Aunt
   Solar
   Diff
   Tree
   Witch
The Append program can be used to append two lists:
append(c(H,T), B, c(H,TB)) <= append(T, B, TB).
append(nil, B, B).

? append( c(1, c(2, nil)), c(3, c(4, nil)), X).

{ Append run Forwards. }

NB. The applet above needs Java on.
 
The same program can also be run "backwards" to take a list apart:
append(c(H,T), B, c(H,TB)) <= append(T, B, TB).
append(nil, B, B).

? append( X, Y, c(1, c(2, c(3, nil))) ).

{ Append run Backwards. }

In this case there are four pairs of lists, X and Y, which give c(1, c(2, c(3, nil))) when appended.
www:


© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Created with "vi (Linux or Solaris)",  charset=iso-8859-1,  fetched Sunday, 26-May-2013 06:02:38 EST.

free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop,
Firefox web-browser, FlashBlock flash on/off.