From: Jérôme Benoit Date: Tue, 25 Apr 2017 18:30:56 +0000 (+0200) Subject: Implement more exercices' functions X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7212827913f4e3b4c1fa577cd866ef1031577477;p=TD_LISP.git Implement more exercices' functions Signed-off-by: Jérôme Benoit --- diff --git a/exercices/lists.lsp b/exercices/lists.lsp new file mode 100755 index 0000000..d82de48 --- /dev/null +++ b/exercices/lists.lsp @@ -0,0 +1,86 @@ +#!/usr/bin/env newlisp + +(setq L '(1 9 3 7 0 5)) +(setq M '(2 6 5 4 0 1 3)) + +(define (mem E L) ;la function mem a 2 arguments + (if (null? L) nil + (if (= (first L) E) true + (mem E (rest L))))) +(println (mem 9 L)) + +(define (boolmemrec E L) + (cond + ((null? L) nil) + ((= (first L) E) true) + ((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 3 L)) + +(define (mem E L) + (cond + ((null? L) nil) + ((= (first L) E) L) + ((mem E (rest L))))) +(println (mem 9 L)) +(println (member 9 L)) +(println (mem 8 L)) +(println (member 8 L)) + +(define (concatene L1 L2) + (if (null? L1) L2 + (cons (first L1) (concatene (rest L1) L2)))) +(println (concatene L M)) +(println (append L M)) + +;(trace true) + +(define (rang x L) + (if (boolmemrec x L) + (cond + ((= x (first L)) 0) + ((+ 1 (rang x (rest L))))) + nil)) +(println (rang 4 L)) +(println (find 4 L)) +(println (rang 0 L)) +(println (find 0 L)) + +;(trace nil) + +(define (tete n L) + (cond + ((null? L) nil) + ((= n 0) nil) + ((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) + ((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)) + +(setq N '(1 9 3)) +(define (inclu L1 L2) + (cond + ((null? L1) true) + ((null? L2) nil) + ((= (first L1) (first L2)) (inclu (rest L1) (rest L2))) + ((inclu L1 (rest L2))))) +(println (inclu N L)) +(println (inclu L M)) +;did not found a newlisp builtin equivalent function + +(exit)