From: Jérôme Benoit Date: Sun, 7 May 2017 20:57:46 +0000 (+0200) Subject: Add the fibonacci two functions X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=dc802a3ec00113c0b214f2e0c716291450c0caa4;p=TD_LISP.git Add the fibonacci two functions Signed-off-by: Jérôme Benoit --- diff --git a/exercices/arithmetic.lsp b/exercices/arithmetic.lsp index 160645e..4bfa94f 100755 --- a/exercices/arithmetic.lsp +++ b/exercices/arithmetic.lsp @@ -60,6 +60,7 @@ (setq L '(3 7 + 4 2 + *)) (setq M '(4 3 7 + * 2 -)) +(setq N '(10 10 5 / +)) (define (calculExp P L) (cond ((null? L) P) @@ -73,6 +74,41 @@ (println (calculExp '() L)) ;(trace true) (println (calculExp '() M)) +(println (calculExp '() N)) + +;(trace nil) + +(setq Q '(+ (* x 0) (* 10 (+ y 0)))) +(define (algsimplificator L) + (cond + ((null? L) '()) + ((= (first L) ) (rest L)) + + )) +(println "algsimplificator") +;(println algsimplificator(Q)) + +(define (fibonacci N) + (cond + ((= N 0) 0) + ((= N 1) 1) + ((> N 1) (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))) +(println "fibonacci") +;(println (fibonacci 21)) +;(println (fibonacci 14)) +(println (fibonacci 20)) +(println (time (fibonacci 20))) + +;(trace true) + +(define (fibo:fibo n) + (if (not fibo:mem) (set 'fibo:mem '(0 1))) + (dotimes (i (- n 1)) + (push (+ (fibo:mem -1) (fibo:mem -2)) fibo:mem -1)) + (last fibo:mem)) +(println "fibo") +(println (fibo 20)) +(println (time (fibo 20))) ;(trace nil)