Advertisement
Nuparu00

roma in italia est

Nov 22nd, 2021
2,396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.71 KB | None | 0 0
  1. ;;Lecture 8
  2.  
  3. ;;1-1
  4. (defun approx-= (a b epsilon)
  5.   (<= (abs (- a b)) epsilon))
  6.  
  7. (defun fun-fixpoint (delta fun)
  8.   (labels ((fixpoint (x)
  9.              (let ((next (funcall fun x)))
  10.                (if (approx-= x next delta)
  11.                    next
  12.                  (fixpoint next)
  13.                  )))) (fixpoint 0)))
  14.  
  15. ;;1-2
  16.  
  17. (defun foldr (fun list init)
  18.   (if (null list)
  19.       init
  20.     (funcall fun (car list) (foldr fun (cdr list) init))))
  21.  
  22. (defun my-length (list)
  23.   (labels ((inc (val next) ;;We will just ignore the actual value in the list - we only care that there is in fact some value at all
  24.              (1+ next)))
  25.     (foldr #'inc list 0)))
  26.  
  27.  
  28. (defun my-make-list (length elem)
  29.   (labels ((make (size ir)
  30.              (if (<= size 0)
  31.                  ir
  32.                (make (1- size) (cons elem ir)) )))
  33.     (make length '() )))
  34.  
  35.  
  36. ;;1-3
  37.  
  38. ;;(defun add-to-all (elem list)
  39. ;;  (if (null list)
  40. ;;      ()
  41. ;;    (cons (cons elem (car list)) (add-to-all elem (cdr list)))))
  42.  
  43. (defun add-to-all (elem list)
  44.   (labels ((construct (x) (cons elem x))) (mapcar #'construct list)))
  45.  
  46. (defun merge-sort (list fun)
  47.   (let* ((len (length list))
  48.          (len/2 (floor (/ len 2)))
  49.          (list2 (nthcdr len/2 list))
  50.          (list1 (ldiff list list2)))
  51.     (if (<= len 1)
  52.         list
  53.       (merge-lists (merge-sort list1 fun) (merge-sort list2 fun) fun))))
  54.  
  55. (defun merge-lists (l1 l2 fun)
  56.   (cond ((null l1) l2)
  57.         ((null l2) l1)
  58.         ((funcall fun (car l1) (car l2))
  59.          (cons (car l1) (merge-lists (cdr l1) l2 fun)))
  60.         (t (cons (car l2) (merge-lists l1 (cdr l2) fun)))))
  61.  
  62. ;;1-4
  63.  
  64. (defun node-value (node)
  65.   (car node))
  66. (defun node-children (node)
  67.   (cdr node))
  68.  
  69. (defun node-value-multi (nodes)
  70.   (if (null nodes)
  71.       '()
  72.     (mapcar #'car nodes)))
  73.  
  74. (defun node-children-multi (nodes)
  75.   (if (null nodes)
  76.       '()
  77.     (labels ((add (x rest) (if (consp x) (cons (cdr x) rest) rest)
  78.         (foldr #'add nodes '()))))))
  79.  
  80.  
  81. ;;2
  82.  
  83. (defun my-find (x list test)
  84.   (cond ((null list) nil)
  85.         ((funcall test x (car list)) (car list))
  86.         (t (my-find x (cdr list) test))))
  87.  
  88. (defun my-find-if (pred list)
  89.   (cond ((null list) nil)
  90.         ((funcall pred (car list)) (car list))
  91.         (t (my-find-if pred (cdr list)))))
  92.  
  93. (defun my-member (x list)
  94.   (cond ((null list) nil)
  95.         ((eql x (car list)) list)
  96.         (t (my-member x (cdr list)))))
  97.  
  98. (defun my-member-if (x list test)
  99.   (cond ((null list) nil)
  100.         ((funcall test x (car list)) list)
  101.         (t (my-member-if x (cdr list) test))))
  102.  
  103. ;;3
  104. (defun my-mapcar (fun list)
  105.   (if (null list)
  106.       '()
  107.     (labels ((wrapper (x y)
  108.                (if (null x) '() (cons (funcall fun x) y))))
  109.       (foldr #'wrapper list '()))))
  110.  
  111. ;;4
  112. (defun foldl (fun list init)
  113.   (if (null list)
  114.       init
  115.     (foldl fun (cdr list) (funcall fun init (car list)))))
  116.  
  117. ;;5
  118. (defun arithmetic-mean (n &rest nums)
  119.   (/ (+ n (foldr #'+ nums 0)) (+ 1 (my-length nums))))
  120.  
  121. ;6
  122. (defun equal-lists-p (a &rest rest)
  123.   (labels ((equals-2-p (a b) (or (and (null a) (null b))
  124.                                  (and (consp a)
  125.                                       (consp b)
  126.                                       (eql (car a) (car b))
  127.                                       (or
  128.                                        (and
  129.                                         (not (cdr a))
  130.                                         (not (cdr b)))
  131.                                        (equals-2-p (cdr a) (cdr b))))))
  132.            (equalsp (a lists) (or (null lists) (and (equals-2-p a (car lists)) (equalsp a (cdr lists)))))
  133.            )
  134.     (equalsp a rest)))
  135.  
  136.  
  137. ;;7
  138.  
  139. (defun my-mapcar (fun &rest rest)
  140.     (labels ((next (lists) (if (or (null lists) ;;Gets a list of the lists without the currently first elements
  141.                                    (null (car lists)))
  142.                                '()
  143.                              (cons (cdar lists) (next (cdr lists)))))
  144.              (pack (lists) (if (or (null lists) ;;Gets a list of the currently first elements
  145.                                    (null (car lists)))
  146.                                '()
  147.                              (cons (caar lists) (pack (cdr lists)))))
  148.              (execute (list) (progn (write list) (apply fun list))) ;;Applies the function on the list of currently first elements
  149.              (run (lists) (if (null lists) ;;Main function loop
  150.                               '()
  151.                             (let ((group (pack lists)))
  152.                               (if (null group)
  153.                                   '()
  154.                                 (cons (execute (pack lists)) (run (next lists))))))))
  155.              
  156.       (run rest))) ;;Start
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement