X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=exercices%2Flists.lsp;h=39114ad8d0ba3f786f287ec5e8c015209ff50add;hb=ada8f303fa63f62209dddeab6f4618f5b73a66b7;hp=d82de48725d0d0524fa3f62aca5b6d0993a1166c;hpb=7212827913f4e3b4c1fa577cd866ef1031577477;p=TD_LISP.git diff --git a/exercices/lists.lsp b/exercices/lists.lsp index d82de48..39114ad 100755 --- a/exercices/lists.lsp +++ b/exercices/lists.lsp @@ -7,6 +7,7 @@ (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) @@ -14,12 +15,14 @@ ((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 "mem rt list") (println (mem 3 L)) (define (mem E L) @@ -27,6 +30,7 @@ ((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)) @@ -35,7 +39,9 @@ (define (concatene L1 L2) (if (null? L1) L2 (cons (first L1) (concatene (rest L1) L2)))) -(println (concatene L M)) +(println "concatene/append") +(setq C (concatene L M)) +(println C) (println (append L M)) ;(trace true) @@ -46,6 +52,7 @@ ((= 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)) @@ -55,21 +62,21 @@ (define (tete n L) (cond - ((null? L) nil) - ((= n 0) nil) + ((null? L) '()) + ((= n 0) '()) ((cons (first L) (tete (- n 1) (rest L)))))) -;FIXME: return nil after the first n elements in the list +(println "tete/slice") (println (tete 3 L)) (println (slice L 0 3)) (println (0 3 L)) -(define (inter L1 L2) +(define (intersectL L1 L2) (cond - ((null? L1) nil) - ((member (first L1) L2) (cons (first L1) (inter (rest L1) L2))) - ((inter (rest L1) L2)))) -;FIXME: return nil after ... -(println (inter L M)) + ((null? L1) '()) + ((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)) @@ -79,8 +86,79 @@ ((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 +(define (unionE L1 L2) + (cond + ((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 (union N L)) + +;(trace true) + +(setq T '(((4)((2 5))((((((6 7))))))))) +(define (prof L) + (cond + ((null? L) 0) + ((atom? L) 0) + ((max (+ 1 (prof (first L))) (prof (rest L)))))) +(println "prof") +(println (prof L)) +(println (prof T)) + +;(trace nil) +;(trace true) + +(define (aplatir L) + (cond + ((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)) + +;(trace nil) + +(define (elim L) + (cond + ((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)) + +(define (reverseL L) + (cond + ((null? L) '()) + ((append (reverseL (rest L)) (list (first L)))))) +(println "reverseL/reverse") +(println (reverseL L)) +(println (reverse L)) + +(define (list< n L) + (cond + ((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)) + +(define (list>= n L) + (cond + ((null? L) '()) + ((< (first L) n) (list>= n (rest L))) + ((cons (first L) (list>= n (rest L)))))) +(println "list>=") +(println (list>= 5 C)) + (exit)