Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. 1.
  2. (defun fetch (key alist)
  3. (cond ((null alist) '?)
  4. ((equal (caar alist) key) (cadar alist))
  5. (t (fetch key (cdr alist)))))
  6. (print (fetch '(denisa misa) '((ana bana) (dana ioana) ((denisa misa) (boma toma)) (ionela ina))))
  7.  
  8. 2.
  9. (defun list-keys (alist)
  10. (cond ((null alist) '())
  11. ((atom (caar alist)) (append (list (caar alist)) (list-keys (cdr alist))))
  12. (t (append (list-keys (car alist)) (list-keys (cdr alist))))))
  13. (print (list-keys '(((marius 1) (nadina 2)) (george 1))))
  14.  
  15. 3.
  16.  
  17. (setf (get 'toma 'dad) 'marius)
  18. (setf (get 'daniel 'dad) 'marian)
  19. (setf (get 'marius 'dad) 'adrian)
  20. (setf (get 'adrian 'dad) 'alex)
  21. (setf (get 'alex 'dad) 'darius)
  22.  
  23. (defun grandpa (sym) (get (get sym 'dad) 'dad))
  24. (print (grandpa 'toma))
  25.  
  26. 4.
  27. (defun adam (sym)
  28. (cond ((null (get (get sym 'dad) 'dad)) (get sym 'dad))
  29. (t (adam (get sym 'dad)))))
  30. (print (adam 'toma))
  31.  
  32. 5.
  33. (defun mom (sym) (get sym 'mom))
  34. (defun dad (sym) (get sym 'dad))
  35.  
  36. (defun ancestors (sym) (cond ((null sym) nil)
  37. (t (append (list sym) (ancestors (mom sym)) (ancestors (dad sym))))))
  38. (print (ancestors 'ana))
  39.  
  40. 6.
  41. (defun list-to-array (l)
  42. (make-array (list (length l)
  43. (length (car l)))
  44. :initial-contents l))
  45.  
  46. (print-array (list-to-array (list (list 0 0 0 ) (list 0 0 0) (list 0 0 0))))
  47.  
  48. OR
  49.  
  50. (setq m (make-array '(3 3) :initial-contents '((0 1 2 ) (3 4 5) (6 7 8))))
  51. (setq space '| |)
  52. (defun print-matrix (m)
  53. (do ((i 0 (+ 1 i)))
  54. ((= i (array-dimension m 0)) t)
  55. (do ((j 0 (+ j 1)))
  56. ((= j (array-dimension m 1)) )
  57. (prin1 (aref m i j))
  58. (princ space))
  59. (terpri)))
  60. (print-matrix m)
  61.  
  62. 7.
  63. (defun find-dim2 (l)
  64. (cond ((null l) 0)
  65. ((atom l) 1)
  66. (t (apply '+ (mapcar 'find-dim2 (car l))))))
  67.  
  68. (defun find-dim1 (l)
  69. (do ((i 0 (+ i 1)))
  70. ((null l) i)
  71. (setq l (cdr l))))
  72.  
  73. (setq l '((1 2) (3 4) (5 6)))
  74. (defun make-matrix (l)
  75. (setq a (find-dim1 l))
  76. (setq b (find-dim2 l))
  77. (setq dim (list (find-dim1 l) (find-dim2 l)))
  78. (setq m (make-array dim))
  79. (do ((i 0 (+ i 1))
  80. (p (car l) (car l)))
  81. ((= i a) m)
  82. (do ((j 0 (+ j 1)))
  83. ((= j b))
  84. (setf (aref m i j) (car p))
  85. (setq p (cdr p)))
  86. (setq l (cdr l))))
  87. (print-matrix (make-matrix l))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement