Advertisement
jradix

Sticks

Oct 11th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.47 KB | None | 0 0
  1. ;; See https://programmingpraxis.com/2016/10/07/sticks/
  2.  
  3. (defun stick (s)
  4.   (setf s (sort s #'<))
  5.   (let ((cost 0) (the-sum 0))
  6.     (do ((x (pop s) (or (null s) (pop s))))
  7.     ((null s) cost)
  8.       (setf the-sum (+ x (car s))
  9.         cost (+ cost the-sum)
  10.         s (insert-sorted the-sum (cdr s))))))
  11.  
  12. (defun insert-sorted (obj lst)
  13.   (if (null lst)
  14.       (cons obj nil)
  15.       (if (< obj (car lst))
  16.       (cons obj lst)
  17.       (cons (car lst) (insert-sorted obj (cdr lst))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement