X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=exercices%2Farithmetic.lsp;h=160645e3840954a726524daacbd51e2339d3c9de;hb=5712270167a05905eed75f7cecb0e49af4225537;hp=a3f27ce90d7c99e6e7ca71575b0b7a16d336c6b0;hpb=c4cb602cbbf151134a81d95e6d07a016521cbb48;p=TD_LISP.git diff --git a/exercices/arithmetic.lsp b/exercices/arithmetic.lsp index a3f27ce..160645e 100755 --- a/exercices/arithmetic.lsp +++ b/exercices/arithmetic.lsp @@ -9,20 +9,22 @@ ((* P (Puissance1 P (- N 1)))))) (println "Puissance1") (println (Puissance1 5 5)) +(println (Puissance1 2 12)) ;(trace true) ;O(log N) (define (Puissance2 P N) (cond - ((= N 0) 1) ((= N 1) P) - ((> N 1) + ((= N 2) (* P P)) + ((> N 2) (cond ((= (mod N 2) 0) (Puissance2 (Puissance2 P 2) (/ N 2))) - ((* P (Puissance2 (Puissance2 P 2) (/ (- 1 N) 2)))))))) + ((* P (Puissance2 (Puissance2 P 2) (/ (- N 1) 2)))))))) (println "Puissance2") -;(println (Puissance2 5 5)) +(println (Puissance2 5 5)) +(println (Puissance2 2 12)) ;(trace nil) @@ -41,29 +43,37 @@ ;(trace true) ; https://fr.wikipedia.org/wiki/Coefficient_binomial -; relation de pascal +; relation de pascal commenté (define (comb N P) (cond ((= P 0) 1) ((= N P) 1) - ((+ (comb (- N 1) P) (comb (- N 1) (- P 1)))))) + ;((+ (comb (- N 1) P) (comb (- N 1) (- P 1)))))) + ((/ (* N (comb (- N 1) (- P 1))) P)))) (println "comb") (println (comb 5 4)) (println (comb 60 4)) +(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)