Intro

  1.  
  2. ;;;;;;;;;;
  3. ;;;    ;;;
  4. ;   2    ;
  5. ;;;    ;;;
  6. ;;;;;;;;;;
  7.  
  8. ;-----------------------------------------
  9. ; factorielle - enveloppe
  10. ; (au lieu de T: (> n 0) aussi possible)
  11. ;-----------------------------------------
  12. (defun fact (n)
  13.       (cond ((= n 0) 1)
  14.             (T (* n (fact (- n 1))))))
  15.  
  16. ;(print (fact 4))
  17. ;-----------------------------------------
  18.  
  19. ;-----------------------------------------
  20. ; factorielle - terminale; appel: (factt n 1)
  21. ;-----------------------------------------
  22. (defun factt (n m)
  23.   (if (= n 0) m
  24.       (factt (- n 1) (* m n))))
  25. ;-----------------------------------------
  26.  
  27.  
  28. ;-----------------------------------------
  29. ; fibo enveloppe, complexite: exp (recursive)
  30. ;-----------------------------------------
  31. (defun fibo (n)
  32.        (cond ((or (eq 0 n) (eq 1 n)) 1 )
  33.              (T (+ (fibo(- n 1)) (fibo (- n 2))))))
  34.  
  35. ;(print (fibo 5))
  36. ;-----------------------------------------
  37.  
  38.  
  39. ;-----------------------------------------
  40. ; fibo - terminale et lineare
  41. ; complexite: O(n)
  42. ; fct.: fibo iterativ,calculates all values bottom up and adds them with this value and the value before that
  43. ;-----------------------------------------
  44. (defun fibonacci (n)
  45.        (fibo_help 1 0 n)
  46. )
  47. (defun fibo_help (x y n)
  48.        (cond ( (= n 0) x )
  49.              ( T (fibo_help (+ x y) x (- n 1)) ))
  50. )
  51.  
  52. ; (trace fibo_help) ;
  53. ; (print (fibonacci 5))
  54. ;-----------------------------------------
  55.  
  56.  
  57. ;;;;;;;;;;
  58. ;;;    ;;;
  59. ;   3    ;
  60. ;;;    ;;;
  61. ;;;;;;;;;;
  62. ; regarder feuilles d'exercices
  63. ;
  64. ;eq : (eq (()) (()) ) --> NIL
  65. ;eql: The eql predicate is true if its arguments are eq, or if they are numbers of the same type with the same value, or if they are
  66. ;     character objects that represent the same character. For example:
  67.  
  68. ;;;;;;;;;;
  69. ;;;    ;;;
  70. ;   4    ;
  71. ;;;    ;;;
  72. ;;;;;;;;;;
  73.  
  74. ;-----------------------------------------
  75. ; member - terminale
  76. ;-----------------------------------------
  77. (defun mymember ( x l)
  78.   (cond ((atom l) ()) ; end der liste, oder leere liste
  79.         ((eql (car l) x) l )
  80.         (T (mymember x (cdr l)))))
  81.  
  82. ;(print (mymember 3 '(1 2 3 4 5)))
  83. ;-----------------------------------------
  84.  
  85.  
  86. ;-----------------------------------------
  87. ; length - enveloppe
  88. ;-----------------------------------------
  89. (defun mylength (l)
  90.   (cond ( (NULL (cdr l)) 1)
  91.         (T (+ 1 (mylength (cdr l))))))
  92.  
  93. ;(print (mylength '(1 2 '(3 no) 4 5)))
  94. ;-----------------------------------------
  95.  
  96. ;-----------------------------------------
  97. ; length - terminale (Nr. 7)
  98. ;-----------------------------------------
  99. (defun mylength2 (list)
  100.   (labels ((helpF (list length)
  101.                   (cond ( (NULL (cdr list)) (+ 1 length))
  102.                         (T (helpF (cdr list) (+ length 1))))))
  103.           (helpF list 0)))
  104. ;(print (mylength2 '(1 2 '(3 no) 4 5)))
  105. ;-----------------------------------------
  106.  
  107. ;-----------------------------------------
  108. ; last - terminale
  109. ;-----------------------------------------
  110. (defun mylast (l)
  111.   (cond ((atom l) ())
  112.         ((eql (cdr l) NIL) (car l))     ;das gleiche: ((atom (cdr l)) l );debuggen:in innere klammer kann (print l) kommen
  113.         (T (mylast (cdr l)))))
  114.  
  115. ;(print (mylast '(1 2 '(3 no) 4 '( 2 3))))
  116.  
  117.  
  118. ;-----------------------------------------
  119. ; makelist(decroissant - absteigend) - enveloppe
  120. ;-----------------------------------------
  121. (defun makelist (n)
  122.   (cond ((= n 0) ())            ; mit ((= n 1) 1) zurückgeben ist unschlau, weil keine schöne liste (ende: .1)
  123.         (T (cons n (makelist (- n 1))))))
  124.  
  125. ;(print (makelist 5))
  126. ;-----------------------------------------
  127.  
  128. ;-----------------------------------------
  129. ; makelist (decroissant - absteigend) - terminale
  130. ; mais avec tric: reverseList a la fin
  131. ;-----------------------------------------
  132. (defun makelist2 (n)
  133.   (labels ((help (list n)
  134.                  (cond ((= n 0) list)
  135.                        (T (help (cons n list) (- n 1)) ))))
  136.            (reverseList (help () n))))
  137.  
  138. ;(print (makelist2 5))
  139. ;-----------------------------------------
  140.  
  141. ;-----------------------------------------
  142. ; makelist revers (croissant - aufsteigend) - terminale
  143. ;-----------------------------------------
  144. (defun makelistrev (n l)
  145.   (cond ((= n 0) l)
  146.         (T (makelistrev (- n 1) (cons n l) )  )))
  147.  
  148. ;(print (makelistrev 5 () ) )
  149. ;-----------------------------------------
  150.  
  151.  
  152. ;-----------------------------------------
  153. ; reverseList : dreht Liste um - terminale
  154. ;-----------------------------------------
  155. (defun reverseList (l)
  156.   (labels ((helpFct (l c)
  157.                     (cond ((atom l) c)
  158.                           (T (helpFct (cdr l) (cons (car l) c)))))); hier hängen wir jeweils das erste element von l vorne dran
  159.           (helpFct l ())))
  160.  
  161. ;(print (reverseList '(1 2 (3 no) 4)))
  162. ;-----------------------------------------
  163.  
  164.  
  165. ;-----------------------------------------
  166. ; reverseList version2 (avec if) - terminale
  167. ;-----------------------------------------
  168.  
  169. (defun myreverse (l)
  170.   (labels ((aux ( ll r )                                        ; definition d'une fonction locale, ll est la vielle cellule
  171.                 (if (atom ll) r    ; r est la copie de la cellul précédent
  172.                     (aux (cdr ll) (cons (car ll) r ) ))))
  173.           (aux l ()) ))
  174.  
  175. ;(trace myreverse)            ; arbeitet mit labels, daher nicht sinnvoll hier
  176. ;(myreverse '(1 2 3 4 5))
  177. ;(untrace myreserse)
  178. ;-----------------------------------------
  179.  
  180.  
  181. ;-----------------------------------------
  182. ; copylist
  183. ;-----------------------------------------
  184. (defun copylist (l)
  185.   (reverseList (reverseList l)))
  186. ;(print (copylist '(1 2 3 4)))
  187. ;-----------------------------------------
  188.  
  189. ;-----------------------------------------
  190. ; copylist version2 - enveloppe
  191. ;-----------------------------------------
  192. (defun copylist2 (l1)
  193.   (cond ((atom (cdr l1)) l1)
  194.         (T (cons (car l1) (copylist2 (cdr l1))  ))))
  195.  
  196. ; (print (copylist2 '(1 2 3 4)))
  197. ;-----------------------------------------
  198.  
  199. ;-----------------------------------------
  200. ; copylist version3 - terminale
  201. ; mais avec tric de reverseList
  202. ;-----------------------------------------
  203. (defun copylist3 (l1 copy)
  204.   (cond ((atom l1) (reverseList copy))
  205.         (T (copylist3 (cdr l1) (cons (car l1) copy) )  )))
  206.  
  207. ;(print (copylist3 '(1 2 3 4) ()))
  208. ;-----------------------------------------
  209.  
  210. ;-----------------------------------------
  211. ; remove (pareil comme delete)
  212. ;-----------------------------------------
  213. (defun myremove (x l)
  214.   (cond ((atom l) l)         
  215.         ((eql (car l) x) (myremove x (cdr l)) )
  216.          (T (cons (car l) (myremove x (cdr l)) )))))
  217.  
  218. ;(print (myremove 3 '( 1 2 3 4 5 6)))
  219. ;-----------------------------------------
  220.  
  221.  
  222. ;-----------------------------------------
  223. ; remove avec les mêmes cellules --> setf -  marche
  224. ;-----------------------------------------
  225. ; wir setzen cdr ll auf delete von cdr ll und geben danach ll normal, dh falls sich nichts ändert, bleibt ll gleich
  226. (defun delete2 (x ll)      ; copie de ll sans les x
  227.   (cond ((atom ll) ll)
  228.         ((eql (car ll) x) (delete2 x (cdr ll)))
  229.         (T (progn (setf (cdr ll) (delete2 x (cdr ll))) ll))))
  230. ;(print (delete2 3 '(1 2 3 4 5 6)))
  231. ;----------------------------------------- 
  232. (setq d '(1 2 3 4 5 6))
  233. (print (delete2 1 d)) ;=> (2 3 4 5 6)
  234. (print d) ; => (1 2 3 4 5 6) ;wieder wie vorher, auch bei (setq d ...), weil d immer noch auf 1 zeigt, ABER:
  235. (print (delete2 2 d)) ;=> (1 3 4 5 6)
  236. (print d) ;=> da is die 2 auch weg...
  237.  
  238.  
  239.  
  240. ;-----------------------------------------
  241. ; append, verbindet nur 2 listen, soll aber ineffizient programmiert sein ^^
  242. ;-----------------------------------------;
  243. (defun myappend (l1 l2)
  244.   (cond ((atom l1)  l2)
  245.         (T (cons (car l1) (myappend (cdr l1) l2) ))))
  246.  
  247. ;(print (myappend '(1 2 3) '(4 5 6) ))
  248. (print (myappend '() '(4 5 6) ))
  249. ;-----------------------------------------
  250.  
  251.  
  252. ;-----------------------------------------
  253. ; append fuer mehrere listen, # kann auch weggelassen werden...
  254. ;-----------------------------------------
  255. (defun myappend2 (l &rest ll)                  ; l = 1ère liste, ll= la liste des autres listes!
  256.   (if (null ll) l                                            ; dann haben wir alle angefügt und geben die liste aus
  257.       (if (atom l) (apply #'myappend2 ll)                                    ; wenn wir alle vorne abge-car-t haben, starten wir mit der nächsten liste
  258.           (cons (car l) (apply #'myappend2 (cdr l) ll)))))   ; und am ende bauen wir sie alle zusammen ==> Trés inefficace!!
  259. ; apply is like funcall, except that its final argument should be a list; the elements of that list are treated as if they were
  260. ; additional arguments to a funcall --> due to apply,, the rest lists are treated as seperate elements, not as lists (which would
  261. ; later be treated as just one element of the final list)
  262. ;(print (myappend2 '(1 2 3) '(4 5 6) '(7 8)))
  263. ;-----------------------------------------
  264.  
  265.  
  266. ;;;;;;;;;;;;;;################################################################################## version avec les même cellule?
  267. ; question: comment avec setf?
  268. ; siehe lösung vorher mit setf  in copytree !!
  269. (defun appendSf (l &rest ll)
  270.   (if
  271.       ))
  272.  
  273. ;-----------------------------------------;
  274. ; adjoin
  275. ; fügt ein element hinzu, falls es nicht schon vorhanden ist, eine mengenoperation
  276. ;-----------------------------------------
  277. (defun madjoin (item list)
  278.   (if (member item list) list
  279.       (cons item list))); else-zweig
  280.  
  281. ;(print (madjoin '4 '(1 2 3)))
  282. ;(print (madjoin '4 '(1 2 3 4)))
  283. ;-----------------------------------------
  284.  
  285.  
  286.  
  287. ;;;;;;;;;;;-----------------------------------------
  288. ;;;    ;;;
  289. ;   5    ;
  290. ;;;    ;;;
  291. ;;;;;;;;;;;-----------------------------------------
  292.  
  293.  
  294.  
  295.  
  296. ;-----------------------------------------
  297. ; treeSize: berechnet die menge aller zellen (also innerer und aeusserer blätter - enveloppe
  298. ; beachte, das weitere verzweigungen immer auch nur ein car sind und das jeweilige cdr eine () ist!
  299. ; (siehe extra zettel)
  300. ;-----------------------------------------
  301. ; ############################################################depends si "()" sont feuilles ou pas aussi numberOfLeaves
  302. (defun treeSize (tree)
  303.   (if (atom tree) 1
  304.       (+ (treeSize (car tree)) (treeSize (cdr tree)))))
  305. ;(trace treeSize)
  306. ;(print (treeSize '( ((1 2) b ) (3 (5 (6 7))) )))
  307. ;(print (treeSize '(a (b c))))
  308. ;-----------------------------------------
  309.  
  310.  
  311.  
  312. ;-----------------------------------------
  313. ; does not work, see version with if - enveloppe
  314. ; numberOfLeaves that are not empty --> leaves with content
  315. ;-----------------------------------------
  316. (defun numberOfLeaves (tree)
  317.   (cond ((and (atom tree) (eq NIL tree)) 0)
  318.         ((and (atom tree) (not (eq NIL tree))) 1)
  319.         (T (+ (numberOfLeaves (car tree)) (numberOfLeaves (cdr tree))) )))
  320. ;trace numberOfLeaves)
  321. ;print (numberOfLeaves '(a (b c))))
  322. ; ################################################################## pourquoi il return 0 toujours?
  323. ; ok
  324.  
  325.  
  326. ;------------------------------------------
  327. ; number of leaves not nil - works - enveloppe
  328. ;------------------------------------------
  329. (defun numberOfLeaves2 (tree)
  330.   (if (and (atom tree) (eq NIL tree)) 0
  331.       (if (and (atom tree) (not (eq NIL tree))) 1
  332.           (+ (numberOfLeaves2 (car tree)) (numberOfLeaves2 (cdr tree))) )))
  333.  
  334. ;(print (numberOfLeaves2 '(a (b c))))
  335.  
  336.  
  337. ;------------------------------------------
  338. ; number of leaves not nil - enveloppe
  339. ;------------------------------------------
  340. (defun numberOfAllLeaves (tree)
  341.       (if (atom tree) 1
  342.           (+ (numberOfAllLeaves (car tree)) (numberOfAllLeaves (cdr tree))) ))
  343.  
  344. ;(print (numberOfAllLeaves '(a (b c))))
  345. ;------------------------------------------
  346.  
  347.  
  348. ;-------------------------------------------
  349. ; copytree - works - enveloppe
  350. ;------------------------------------------
  351. (defun copytree (tree)
  352.   (if (atom tree) tree
  353.       (cons (copytree (car tree)) (copytree (cdr tree)))))
  354. ;------------------------------------------
  355.  
  356. ;------------------------------------------
  357. ; substitute: replaces all x for y in tree - works - enveloppe
  358. ;------------------------------------------
  359. (defun mySubst (x y tree)
  360.   (if (eql tree x) y
  361.       (if (atom tree) tree
  362.           (cons (mySubst x y (first tree)) (mySubst x y (rest tree))))))
  363.  
  364. ;(print (mySubst 'a '1 '((a 2) ((3 a) (4 5)))))
  365. ;------------------------------------------
  366.  
  367.  
  368. ;------------------------------------------
  369. ; treeLeaves - returns list with leaves with NIL - works
  370. ;------------------------------------------
  371. ; lazy version: (defun treeLeavesLazy (tree) (collect-append tree))
  372. ; il ne connait pas cette fonction, mais les 2 sont gnu cl...
  373.  
  374. ;------------------------------------------
  375. (defun treeLeaves (tree list)
  376.   (if (atom tree) (cons tree list)
  377.       (append (treeLeaves (first tree) list ) (treeLeaves (rest tree) list) )))
  378.  
  379. ;(print (treeLeaves '((a 2) ((3 a) ((3 4) 5)))  () ))
  380. ;------------------------------------------
  381.  
  382.  
  383.  
  384.  
  385.  
  386. ;;;;;;;;;;;-----------------------------------------
  387. ;;;    ;;;
  388. ;   6    ;
  389. ;;;    ;;;
  390. ;;;;;;;;;;;-----------------------------------------
  391.  
  392. ;(eq x y) is true if and only if x and y are the same identical object.
  393.  
  394. ; The eql predicate is true if its arguments are eq, or if they are numbers of the same type with the same value,
  395. ;   or if they are character objects that represent the same character.
  396.  
  397. ; The equal predicate is true if its arguments are structurally similar (isomorphic) objects.
  398. ;    A rough rule of thumb is that two objects are equal if and only if their printed representations are the same.
  399.  
  400. ;-----------------------------------------
  401. ; function equal:
  402. ;-----------------------------------------
  403. (defun myequal (list1 list2)
  404.   (if (and (atom list1) (atom list2) (eql list1 list2) ) T
  405.       (if (and (not (atom list1)) (atom list2)) NIL
  406.           (if (and (not (atom list1)) (atom list2)) NIL
  407.               (if (and (not (atom list1)) (not(atom list2))) 
  408.                   (and (myequal (car list1) (car list2)) (myequal (cdr list1) (cdr list2))))))))
  409.  
  410. ;(trace myequal)
  411. ;(print (myequal '(((1 3) 4 ) (4 5)) '(((1 g) 4 ) (f g)) ))
  412. ;(print (myequal '(((1 3) 4 ) (4 5)) '(((1 3) 4 ) (4 5)) ))
  413. ;-----------------------------------------
  414.  
  415.  
  416.  
  417.  
  418.  
  419. ;;;;;;;;;;;-----------------------------------------
  420. ;;;    ;;;
  421. ;   7    ;
  422. ;;;    ;;;
  423. ;;;;;;;;;;;-----------------------------------------
  424.  
  425. ; solutions pour fonctions terminales sur les arbre sont just a cote des fonctions enveloppe
  426.  
  427. ;-----------------------------------------
  428. ; treeSize: - partiel terminale!
  429. ;-----------------------------------------
  430. (defun treeSize2 (tree size)
  431.   (if (atom tree) size
  432.       (treeSize2 (car tree) (treeSize2 (cdr tree) (+ size 1)))))
  433.  
  434. ;(trace treeSize2)
  435. ;(print (treeSize2 '(a (b c)) 0) )
  436. ; ----------------------------------#################################### return only 4, error ?
  437. ;-----------------------------------------
  438.  
  439. ;-------------------------------------------
  440. ; copytree - partiel terminale! ==> ## n'exist pas, impossible ##
  441. ;------------------------------------------
  442. ;(defun copytree2 (tree rTree)
  443. ;  (if (atom tree) tree
  444. ;      (copytree2 (car tree)  (cons (copytree2 (cdr tree) (cons (car tree) rTree))))))
  445.  
  446. ;------------------------------------------
  447. ;(trace copytree2)
  448. ;(print (copytree2 '(a (b c)) 0) )
  449.  
  450. ;------------------------------------------
  451. ; treeLeaves - returns list with leaves with NIL
  452. ; - partiel terminale!
  453. ;------------------------------------------
  454. ; lazy version: (defun treeLeavesLazy (tree) (collect-append tree)) ; il ne connait pas cette fonction, mais les 2 sont gnu cl...
  455.  
  456. ;------------------------------------------
  457. (defun treeLeaves2 (tree list)
  458.   (if (atom tree) (cons tree list)
  459.       (append (treeLeaves2 (first tree) list ) (treeLeaves2 (rest tree) list) )))
  460.  
  461. ;(print (treeLeaves2 '((a 2) ((3 a) ((3 4) 5)))  () ))
  462. ;------------------------------------------
  463.  
  464.  
  465.  

Leave Comments & Responses

Feel free to leave a comment here. The latest comment is on top.

Add Comment
(and click save twice
with correct code) 
Sign as Author 

Sandra-kd?25 August 2007, 23:16

<a href= ></a>

Vilyamji?02 August 2007, 05:43

Hello! great idea of color of this siyte!