(defun operation (opertype x1 x2) (if (eql opertype 0) (+ x1 x2) (* x1 x2) ) ) (defun operinverse (x1 x2 opertype) (if (eql opertype 0) (- x2 x1) (if (eql x1 0) NIL (/ x2 x1)) ;** Weg div by zero zu vermeiden ** ) ) (defun shorten (liste step) ;** kürzt Liste um step Elemente ** (if (eql step 0) liste (shorten (cdr liste) (- step 1)) ) ) (defun listoper (liste opertype step) ;** verknüft das n und das n-1 Element der Liste mit der inveresen Operation ** ;** aus den Ergebnissen entsteht eine neue Liste ** (if (or (eql liste NIL) (eql (cdr liste) NIL)) NIL (let((inv (operinverse (car liste) (car (cdr liste)) opertype))) (if (eql inv NIL) NIL (cons inv (listoper (shorten liste step) opertype step)) ) ) ) ) (defun algo (liste step) ;** Der eigentliche Algorithmus ** (defun allequal (liste) ;** Prüft ob alle Element der Liste gleich sind ** (if (or (eql liste NIL) (eql (cdr liste) NIL)) T (if (eql (car liste) (car(cdr liste))) (allequal (cdr liste)) NIL ) ) ) (defun loops (liste step opertype) (if (eql opertype 2) NIL ;** wenn bereits alle Operatoen benutz wurden, brich ab ** (let ((solution (algo (listoper liste opertype step) 1))) ;** wende listoper auf Liste an und rufe diese neue Liste mit algo auf ** (if (eql solution NIL) (loops liste step (+ opertype 1)) ;** Wenn das Solution NIL ist , benutze nächsten Operator ** (operation opertype solution (car (last liste))) ;** verknüpfe Solution und das letzte Element der Liste ** ;** gebe diesen Wert zurück ** ) ) ) ) (if (eql (length liste) 1) NIL ;** Wenn die Folge aus genau zwei Elementen besteht, brich ab ** (if (allequal liste) (car liste) ;** Wenn die Folge konstant ist, dann gebe ein Element der Folge ; zurück ** (loops liste step 0) ;** wende den Algorithmus mit verschiednen Operatoren an ** ) ) ) (defun alle (liste step) (defun inner (liste step n) (let ((algooperation (algo (shorten liste (- step 1)) n))) (if (eql algooperation NIL) NIL (if (eql step n) (cons algooperation NIL) (cons algooperation (inner liste (+ step 1) n)) ) ) ) ) (let ((solution (inner liste 1 step))) (if (eq (length solution) step) (nth (mod (- (length liste) 1) step) solution) (if (eql step 4) NIL (alle liste (+ step 1)) ) ) ) ) (load "leser.lsp") (loop for n from 1 to 20 do (let ((akt (explode(lieszeile "numbers.txt" n)))) (print akt) (print (alle (reverse(cdr (reverse akt))) 1)) ) )