From: Jérôme Benoit Date: Sun, 7 May 2017 19:06:03 +0000 (+0200) Subject: Add the arthmetic suffixed expression calculator function X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5712270167a05905eed75f7cecb0e49af4225537;p=TD_LISP.git Add the arthmetic suffixed expression calculator function Signed-off-by: Jérôme Benoit --- diff --git a/exercices/arithmetic.lsp b/exercices/arithmetic.lsp index d3b6cef..160645e 100755 --- a/exercices/arithmetic.lsp +++ b/exercices/arithmetic.lsp @@ -56,18 +56,24 @@ (println "(comb 12 8) = "(comb 12 8)) ;(trace nil) +;(trace true) (setq L '(3 7 + 4 2 + *)) -(setq P '()) +(setq M '(4 3 7 + * 2 -)) (define (calculExp P L) (cond - ((null? L) 0) - ((= (first L) '+) (+ (first P) (calculExp (rest P) (rest L)))) - ((= (first L) '-) (- (first P) (calculExp (rest P) (rest L)))) - ((= (first L) '*) (* (first P) (calculExp (rest P) (rest L)))) + ((null? L) P) + ((= (first L) '+) (calculExp (cons (+ (first P) (P 1)) (rest (rest P))) (rest L))) + ((= (first L) '-) (calculExp (cons (- (P 1) (first P)) (rest (rest P))) (rest L))) + ((= (first L) '*) (calculExp (cons (* (first P) (P 1)) (rest (rest P))) (rest L))) ;FIXME: test for divide by zero - ((= (first L) '/) (/ (first P) (calculExp (rest P) (rest L)))) - ((cons (first L) (calculExp P (rest L)))))) -;(println (calculExp P L)) + ((= (first L) '/) (calculExp (cons (/ (P 1) (first P)) (rest (rest P))) (rest L))) + ((calculExp (cons (first L) P) (rest L))))) +(println "calculExp") +(println (calculExp '() L)) +;(trace true) +(println (calculExp '() M)) + +;(trace nil) (exit)