From: Jérôme Benoit Date: Tue, 25 Apr 2017 21:48:39 +0000 (+0200) Subject: Add more exercices functions X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7cbb4caf073698a0bc7b27a406fe37c31d56d50f;p=TD_LISP.git Add more exercices functions And address some FIXME while at it Signed-off-by: Jérôme Benoit --- diff --git a/exercices/lists.lsp b/exercices/lists.lsp index d82de48..1930b1c 100755 --- a/exercices/lists.lsp +++ b/exercices/lists.lsp @@ -35,7 +35,8 @@ (define (concatene L1 L2) (if (null? L1) L2 (cons (first L1) (concatene (rest L1) L2)))) -(println (concatene L M)) +(setq C (concatene L M)) +(println C) (println (append L M)) ;(trace true) @@ -55,20 +56,18 @@ (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 3 L)) (println (slice L 0 3)) (println (0 3 L)) (define (inter L1 L2) (cond - ((null? L1) nil) + ((null? L1) '()) ((member (first L1) L2) (cons (first L1) (inter (rest L1) L2))) ((inter (rest L1) L2)))) -;FIXME: return nil after ... (println (inter L M)) (println (intersect L M)) @@ -83,4 +82,66 @@ (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 L M)) +(println (unionE 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 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 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 C)) +(println (unique C)) + +(define (reverseL L) + (cond + ((null? L) '()) + ((append (reverseL (rest L)) (list (first L)))))) +(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< 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>= 5 C)) + (exit)