fork download
  1. (defun find-second-smallest-number (list)
  2. (loop for n in list
  3. for second = n then (if (< first n second) n second)
  4. minimize n into first
  5. finally (return (if (> second first) second nil))))
  6.  
  7. (defun test (fn)
  8. (loop for (arg expected) in '(((4 5 1 7 1 2 8 9 2 7) 2)
  9. ((2 2 2 2 2 2 2 2 2 2) nil)
  10. ((1) nil)
  11. ((2 1) 2)
  12. ((1 2) 2)
  13. ((3 2 1) 2)
  14. (() nil))
  15. for result = (handler-case (funcall fn arg)
  16. (error (cond) cond))
  17. do (format t "~:[❌~;✓~] ~:S → ~S~%"
  18. (eql result expected)
  19. arg
  20. result)))
  21.  
  22. (test #'find-second-smallest-number)
  23.  
Success #stdin #stdout 0.01s 29672KB
stdin
Standard input is empty
stdout
✓ (4 5 1 7 1 2 8 9 2 7) → 2
✓ (2 2 2 2 2 2 2 2 2 2) → NIL
✓ (1) → NIL
✓ (2 1) → 2
❌ (1 2) → NIL
❌ (3 2 1) → 3
❌ () → #<TYPE-ERROR expected-type: NUMBER datum: NIL>