Условия IF и COND - Lisp

Узнай цену своей работы

Формулировка задачи:

Всегда ли IF можно заменить COND? какие особенности замены в рекурсий?
Листинг программы
  1. (defun make-heap (&optional (length 7))
  2. (make-array length :adjustable t :fill-pointer 0))
  3. (defun left-index (index)
  4. (1- (* 2 (1+ index))))
  5. (defun right-index (index)
  6. (* 2 (1+ index)))
  7. (defun parent-index (index)
  8. (floor (1- index) 2))
  9. (defun percolate-up (heap index predicate)
  10. (if (zerop index) heap
  11. (do* ((element (aref heap index))
  12. (index index pindex)
  13. (pindex (parent-index index)
  14. (parent-index index)))
  15. ((zerop index) heap)
  16. (if (funcall predicate element (aref heap pindex))
  17. (rotatef (aref heap index) (aref heap pindex))
  18. (return-from percolate-up heap)))))
  19. (defun heap-insert (heap element predicate)
  20. (let ((index (vector-push-extend element heap 2)))
  21. (percolate-up heap index predicate)))
  22. (defun percolate-down (heap index predicate)
  23. (let ((length (length heap))
  24. (element (aref heap index)))
  25. (flet ((maybe-element (index)
  26. "return the element at index or nil, and a boolean
  27. indicating whether there was an element."
  28. (if (< index length)
  29. (values (aref heap index) t)
  30. (values nil nil))))
  31. (do ((index index swap-index)
  32. (lindex (left-index index) (left-index index))
  33. (rindex (right-index index) (right-index index))
  34. (swap-index nil) (swap-child nil))
  35. (nil)
  36. ;; Extact the left child if there is one. If there is not,
  37. ;; return the heap. Set the left child as the swap-child.
  38. (multiple-value-bind (lchild lp) (maybe-element lindex)
  39. (if (not lp) (return-from percolate-down heap)
  40. (setf swap-child lchild
  41. swap-index lindex))
  42. ;; Extract the right child, if any, and when better than the
  43. ;; current swap-child, update the swap-child.
  44. (multiple-value-bind (rchild rp) (maybe-element rindex)
  45. (when (and rp (funcall predicate rchild lchild))
  46. (setf swap-child rchild
  47. swap-index rindex))
  48. ;; If the swap-child is better than element, rotate them,
  49. ;; and continue percolating down, else return heap.
  50. (if (not (funcall predicate swap-child element))
  51. (return-from percolate-down heap)
  52. (rotatef (aref heap index) (aref heap swap-index)))))))))
  53. (defun heap-empty-p (heap)
  54. (eql (length heap) 0))
  55. (defun heap-delete-min (heap predicate)
  56. (assert (not (heap-empty-p heap)) () "Can't pop from empty heap.")
  57. (prog1 (aref heap 0)
  58. (setf (aref heap 0) (vector-pop heap))
  59. (unless (heap-empty-p heap)
  60. (percolate-down heap 0 predicate))))
  61. (defun heapsort (sequence predicate)
  62. (let ((h (make-heap (length sequence))))
  63. (map nil #'(lambda (e) (heap-insert h e predicate)) sequence)
  64. (map-into sequence #'(lambda () (heap-delete-min h predicate)))))

Решение задачи: «Условия IF и COND»

textual
Листинг программы
  1. (or b c)

Объяснение кода листинга программы

  1. Создается новая переменная b и присваивается ей значение истина (t).
  2. Создается новая переменная c и присваивается ей значение ложь (nil).
  3. Используется конструкция or, которая возвращает первое истинное значение.
  4. Значение переменной b сравнивается с истиной (t).
  5. Если значение переменной b равно истине (t), то возвращается значение переменной b.
  6. Если значение переменной b не равно истине (t), то выполняется блок кода, заключенный в конструкцию cond.
  7. В блоке кода используется условное выражение истина (t), которое проверяет значение переменной c.
  8. Если значение переменной c равно истине (t), то возвращается значение переменной c.
  9. Если значение переменной c не равно истине (t), то возвращается значение переменной c, которое было присвоено в шаге 2.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.727 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут