/** Implementiert Listen als Werte * Analog zu LISP * Zugriffsfunktionen: cons, car, cdr, isEmpty */ public class Liste{ // +---+ +-----+------+ +-----+------+ +-----+------+ / // | °-+----->| | °--+----->| | °--+---> ... -->| | °--+----->/ // +---+ +-----+------+ +-----+------+ +-----+------+ / // start ... Zellen ... Zelle start; /* Konstruktoren */ Liste(){ start=null;} Liste(Zelle c) { start=c;} /* Wir verwenden die LISP-Notation car, cdr, cons */ /** Erstes Element*/ String car(){ return start.cont; } /** Restliste*/ Liste cdr(){ return new Liste(start.next);} /** Liste mit zusätzlichem Element */ Liste cons(String s){ return new Liste(new Zelle(s,start));} /**Prädikat*/ boolean isEmpty(){ return (start==null);} /** Liste mit zusätzlicher Folge neuer Elemente - revers */ Liste pushAllReverse(String[] p){ Zelle current = start; for(int k=p.length-1;k>=0;k--) current = new Zelle(p[k],current); return new Liste(current); } /** Ersetzt oberstes Element durch Folge neuer Elemente - revers */ Liste mit(String[] s){ return cdr().pushAllReverse(s); } // Innere Klasse Zelle // +-----+------+ +-----+------+ +-----+------+ / // | | °--+----->| | °--+--> ... --->| | °--+----->/ // +-----+------+ +-----+------+ +-----+------+ / // cont next class Zelle{ String cont; Zelle next; Zelle(String cont,Zelle next){this.cont=cont; this.next=next;} } // end Zelle }