More list exercise functions
[TD_LISP.git] / exercices / lists.lsp
index 1930b1ca3a6e1b18cdf0300b80c11b6bec5a0299..25360bcf166d051570501485b0e2ed6ddb61c5ca 100755 (executable)
@@ -7,6 +7,7 @@
   (if (null? L) nil
     (if (= (first L) E) true
       (mem E (rest L)))))
   (if (null? L) nil
     (if (= (first L) E) true
       (mem E (rest L)))))
+(println "mem rt bool")
 (println (mem 9 L))
 
 (define (boolmemrec E L)
 (println (mem 9 L))
 
 (define (boolmemrec E L)
     ((null? L) nil)
     ((= (first L) E) true)
     ((mem E (rest L)))))
     ((null? L) nil)
     ((= (first L) E) true)
     ((mem E (rest L)))))
+(println "mem rec rt bool")
 (println (boolmemrec 10 L))
 
 (define (mem E L) ;la function mem a 2 arguments
   (if (null? L) nil
     (if (= (first L) E) L
       (mem E (rest L)))))
 (println (boolmemrec 10 L))
 
 (define (mem E L) ;la function mem a 2 arguments
   (if (null? L) nil
     (if (= (first L) E) L
       (mem E (rest L)))))
+(println "mem rt list")
 (println (mem 3 L))
 
 (define (mem E L)
 (println (mem 3 L))
 
 (define (mem E L)
@@ -27,6 +30,7 @@
     ((null? L) nil)
     ((= (first L) E) L)
     ((mem E (rest L)))))
     ((null? L) nil)
     ((= (first L) E) L)
     ((mem E (rest L)))))
+(println "mem rec rt list")
 (println (mem 9 L))
 (println (member 9 L))
 (println (mem 8 L))
 (println (mem 9 L))
 (println (member 9 L))
 (println (mem 8 L))
@@ -35,6 +39,7 @@
 (define (concatene L1 L2)
   (if (null? L1) L2
     (cons (first L1) (concatene (rest L1) L2))))
 (define (concatene L1 L2)
   (if (null? L1) L2
     (cons (first L1) (concatene (rest L1) L2))))
+(println "concatene/append")
 (setq C (concatene L M))
 (println C)
 (println (append L M))
 (setq C (concatene L M))
 (println C)
 (println (append L M))
@@ -47,6 +52,7 @@
       ((= x (first L)) 0)
       ((+ 1 (rang x (rest L)))))
     nil))
       ((= x (first L)) 0)
       ((+ 1 (rang x (rest L)))))
     nil))
+(println "rang/find")
 (println (rang 4 L))
 (println (find 4 L))
 (println (rang 0 L))
 (println (rang 4 L))
 (println (find 4 L))
 (println (rang 0 L))
     ((null? L) '())
     ((= n 0) '())
     ((cons (first L) (tete (- n 1) (rest L))))))
     ((null? L) '())
     ((= n 0) '())
     ((cons (first L) (tete (- n 1) (rest L))))))
+(println "tete/slice")
 (println (tete 3 L))
 (println (slice L 0 3))
 (println (0 3 L))
 
 (println (tete 3 L))
 (println (slice L 0 3))
 (println (0 3 L))
 
-(define (inter L1 L2) 
+(define (intersectL L1 L2) 
   (cond
     ((null? L1) '())
   (cond
     ((null? L1) '())
-    ((member (first L1) L2) (cons (first L1) (inter (rest L1) L2)))
-    ((inter (rest L1) L2))))
-(println (inter L M))
+    ((member (first L1) L2) (cons (first L1) (intersectL (rest L1) L2)))
+    ((intersectL (rest L1) L2))))
+(println "intersectL/intersect")
+(println (intersectL L M))
 (println (intersect L M))
 
 (setq N '(1 9 3))
 (println (intersect L M))
 
 (setq N '(1 9 3))
@@ -78,6 +86,7 @@
     ((null? L2) nil)
     ((= (first L1) (first L2)) (inclu (rest L1) (rest L2))) 
     ((inclu L1 (rest L2)))))
     ((null? L2) nil)
     ((= (first L1) (first L2)) (inclu (rest L1) (rest L2))) 
     ((inclu L1 (rest L2)))))
+(println "inclu")
 (println (inclu N L))
 (println (inclu L M))
 ;did not found a newlisp builtin equivalent function
 (println (inclu N L))
 (println (inclu L M))
 ;did not found a newlisp builtin equivalent function
     ((null? L1) L2)
     ((member (first L1) L2) (unionE (rest L1) L2))
     ((cons (first L1) (unionE (rest L1) L2)))))
     ((null? L1) L2)
     ((member (first L1) L2) (unionE (rest L1) L2))
     ((cons (first L1) (unionE (rest L1) L2)))))
+(println "unionE/union")
 (println (unionE L M))
 (println (unionE N L))
 (println (unionE L M))
 (println (unionE N L))
+(println (union N L))
 
 ;(trace true)
 
 
 ;(trace true)
 
     ((null? L) 0) 
     ((atom? L) 0) 
     ((max (+ 1 (prof (first L))) (prof (rest L))))))
     ((null? L) 0) 
     ((atom? L) 0) 
     ((max (+ 1 (prof (first L))) (prof (rest L))))))
+(println "prof")
 (println (prof L))
 (println (prof T))
 
 (println (prof L))
 (println (prof T))
 
     ((null? L) nil)
     ((atom? L) (list L)) ;FIXME: the "casting" is not working properly
     ((append (aplatir (first L)) (aplatir (rest L))))))
     ((null? L) nil)
     ((atom? L) (list L)) ;FIXME: the "casting" is not working properly
     ((append (aplatir (first L)) (aplatir (rest L))))))
+(println "aplatir/flat")
 ;(println (aplatir T))
 (println (flat T))
 
 ;(println (aplatir T))
 (println (flat T))
 
     ((null? L) '())
     ((member (first L) (rest L)) (elim (rest L)))
     ((cons (first L) (elim (rest L))))))
     ((null? L) '())
     ((member (first L) (rest L)) (elim (rest L)))
     ((cons (first L) (elim (rest L))))))
+(println "elim/unique")
 (println (elim C))
 (println (unique C))
 
 (println (elim C))
 (println (unique C))
 
   (cond
     ((null? L) '())
     ((append (reverseL (rest L)) (list (first L))))))
   (cond
     ((null? L) '())
     ((append (reverseL (rest L)) (list (first L))))))
+(println "reverseL/reverse")
 (println (reverseL L))
 (println (reverse L))
 
 (println (reverseL L))
 (println (reverse L))
 
     ((null? L) '())
     ((>= (first L) n) (list< n (rest L)))
     ((cons (first L) (list< n (rest L))))))
     ((null? L) '())
     ((>= (first L) n) (list< n (rest L)))
     ((cons (first L) (list< n (rest L))))))
+(println "list<")
 (println (list< 5 L))
 (println (list< 5 C))
 
 (println (list< 5 L))
 (println (list< 5 C))
 
     ((null? L) '())
     ((< (first L) n) (list>= n (rest L)))
     ((cons (first L) (list>= n (rest L))))))
     ((null? L) '())
     ((< (first L) n) (list>= n (rest L)))
     ((cons (first L) (list>= n (rest L))))))
+(println "list>=")
 (println (list>= 5 C))
 
 (println (list>= 5 C))
 
+;(trace true)
+
+(define (qsort L)
+  (cond
+    ((null? L) '())
+    ((append 
+       ;the pivot is the first element of the list
+       (qsort (list< (first L) (rest L))) 
+       (cons (first L) '()) 
+       (qsort (list>= (first L) (rest L)))))))
+(println "qsort")
+(println (qsort C))
+
+;(trace nil)
+
+;we suppose both lists are flat
+(setq L1 '(A ? ? B C D ?))
+(setq L2 '(? A B ? C ? ? D))
+
+(define (removeC c L)
+  (cond
+    ((null? L) '())
+    ((= c (first L))) (removeC c (rest L))
+    (((cons (first L) (removeC c (rest L)))))))
+(println (removeC A L1))
+
+(define (filtre L1 L2 C)
+  (cond 
+    ((and (null? L1) (null? L2)) true)
+    ((and (not (null? L1)) (null? L2)) nil)
+    ((and (null? L1) (not (null? L2))) nil)
+    ((and (inclu L1 L2) (inclu L2 L1) true))))
+;(println "filtre")
+;(println (filtre L1 L2 ?))
+
 (exit)
 (exit)