fork download
  1. (defun make-math-expr (operand-list)
  2. (mapcar #'reverse
  3. (reduce (lambda (expr-list operand)
  4. (mapcan (lambda (expr)
  5. (mapcar (lambda (operator)
  6. `(,operand ,operator ,@expr))
  7. '(+ - * /)))
  8. expr-list))
  9. (cdr operand-list)
  10. :initial-value `((,(car operand-list))))))
  11.  
  12. (defun math-to-s-expr (expr)
  13. (mapc (lambda (operator)
  14. (loop with stack
  15. for x = (pop expr)
  16. while expr
  17. do (push (if (find x operator)
  18. (list x (pop stack) (pop expr))
  19. x)
  20. stack)
  21. finally (setf expr (nreverse (cons x stack)))))
  22. '((* /) (+ -)))
  23. (car expr))
  24.  
  25. (defun f228 (operand-list answer)
  26. (format t "~{~a~%~}"
  27. (delete answer (make-math-expr operand-list)
  28. :test-not #'=
  29. :key (lambda (expr) (eval (math-to-s-expr expr))))))
  30.  
  31. (f228 '(1 5 3 6) 10)
  32. (f228 '(1 5 3 6 7) 10)
Success #stdin #stdout 0.03s 10768KB
stdin
Standard input is empty
stdout
(1 + 5 * 3 - 6)
(1 * 5 / 3 * 6)
(1 + 5 + 3 - 6 + 7)