All pastes #2093458 Raw Edit

Untitled

public text v1 · immutable
#2093458 ·published 2011-11-10 00:34 UTC
rendered paste body
(defun all-operations (a b)
	(loop for op in '(+ - * /) collect (list op a b)))

(defun extract (ll)
	(if (null ll) 
	    ll
	    (cons ll (loop for l in (extract (cdr ll)) collect (cons (car l) (cons (car ll) (cdr l))))))) 
			
(defun extract-pairs (ll)
	(loop for e1 in (extract ll) nconc 
	     (loop for e2 in (extract (cdr e1)) collect (cons (cons (car e1) (car e2)) (cdr e2)))))
		
(defun build-solution (what front) 
	(let* ((curr (car front))
	(new-front (loop for pair-and-rest in (extract-pairs curr)
			 nconcing
			 (loop for op in (all-operations (caar pair-and-rest) (cdar pair-and-rest))
			       for next-level = (cons op (cdr pair-and-rest))
			       if (null (cdr pair-and-rest))
			           when (= (handler-case (eval op) (division-by-zero nil 0)) what) 
				        do (return-from build-solution op) 
				   end
			       else collect next-level))))
	(build-solution what (append (cdr front) new-front))))

(defun main () (build-solution 21 '((1 5 6 7))))