Add Somme3ou5 function
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 8 May 2017 19:54:20 +0000 (21:54 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 8 May 2017 19:54:20 +0000 (21:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
exercices/arithmetic.lsp

index f020099e8ea8be02431afe00dcc9d298375155c3..0d55480f69eb758f476ebd94033c8dcf0a8612ad 100755 (executable)
@@ -70,7 +70,7 @@
     ((= (first L) '*) (calculExp (cons (* (P 1) (first P)) (rest (rest P))) (rest L)))
     ;FIXME: test for divide by zero
     ((= (first L) '/) (calculExp (cons (/ (P 1) (first P)) (rest (rest P))) (rest L)))
     ((= (first L) '*) (calculExp (cons (* (P 1) (first P)) (rest (rest P))) (rest L)))
     ;FIXME: test for divide by zero
     ((= (first L) '/) (calculExp (cons (/ (P 1) (first P)) (rest (rest P))) (rest L)))
-    ((number? (first L)) (calculExp (cons (first L) P) (rest L)))))    
+    ((number? (first L)) (calculExp (cons (first L) P) (rest L)))))
 (println "calculExp")
 (println (calculExp '() L))
 ;(trace true)
 (println "calculExp")
 (println (calculExp '() L))
 ;(trace true)
 (define (algsimplificator L)
   (cond
     ((null? L) '())
 (define (algsimplificator L)
   (cond
     ((null? L) '())
+    ; I'm having hard time to find a way of escaping the '(' and ')' characters 
     ((= (first L) ) (rest L))
     ((= (first L) ) (rest L))
+    ;here is the idea: detect the lower well formed expression: begin with (op and finish with ) where op = + - * / and have only two parameters that are atoms.
+    ;then if it match a know pattern, simplify it by following the matching rule.
+    ;do it again on the upper layer recursively until we only have (op A B) that just match no known simplication rules.
 
     ))
 (println "algsimplificator")
 
     ))
 (println "algsimplificator")
 (define (fibo:fibo n)
   (if (not fibo:mem) (set 'fibo:mem '(0 1)))
   (dotimes (i (- n 1))
 (define (fibo:fibo n)
   (if (not fibo:mem) (set 'fibo:mem '(0 1)))
   (dotimes (i (- n 1))
+    ;this create a LIFO (or stack) of all previous fibonnaci serie result values
     (push (+ (fibo:mem -1) (fibo:mem -2)) fibo:mem -1))
   (last fibo:mem))
 (println "fibo")
 (println (fibo 20))
 (println (time (fibo 20)))
 
     (push (+ (fibo:mem -1) (fibo:mem -2)) fibo:mem -1))
   (last fibo:mem))
 (println "fibo")
 (println (fibo 20))
 (println (time (fibo 20)))
 
+;(trace nil)
+;(trace true)
+
+(setq S '())
+(define (Somme3ou5 N)
+    (dolist (i (sequence 0 N))
+      (cond
+        ((= (mod i 3) 0) (setq S (cons i S)))
+        ((= (mod i 5) 0) (setq S (cons i S)))))
+    (apply + S))
+(println "Somme3ou5")
+(println (Somme3ou5 100))
+
 ;(trace nil)
 
 (exit)
 ;(trace nil)
 
 (exit)