[ create a new paste ] login | about

Link: http://codepad.org/KEGK3PeE    [ raw code | fork ]

Scheme, pasted on Aug 14:
(require 'srfi-1)

(define (rotate lst)
  (cond ((null? lst) '())
	(else 
	 (let ((rev (reverse lst)))
	   (cons (car rev) (reverse! (cdr rev)))))))

(define (4sum lst ::list)
  (define (find-4sum l ::list)
    (let ((first-four (take l 4)))
      (cond ((equal? l lst)
	     'no-more-results)
	    ((zero? (apply + first-four))
	     (begin (format #t "~A~%" first-four)
		    (find-4sum (rotate l))))
	    (else
	     (find-4sum (rotate l))))))
  (cond ((member 0 lst)
	 (begin (format #t "~A~%" '(0 0 0 0))
		(find-4sum (rotate lst))))
	((zero? (apply + (take lst 4)))
	 (begin (format #t "~A~%" (take lst 4))
		(find-4sum (rotate lst))))
	(else
	 (find-4sum (rotate lst)))))

;; Eg.
#|kawa:1|# (4sum '(2 3 1 0 -4 -1))
(0 0 0 0)
(-4 -1 2 3)
(3 1 0 -4)
no-more-results


Create a new paste based on this one


Comments: