fork(1) download
  1. ; split an array
  2.  
  3. (define (sum xs) (apply + xs))
  4.  
  5. (define (split-equal xs)
  6. (let ((n (sum xs)))
  7. (if (odd? n) #f
  8. (let loop ((xs xs) (n (/ n 2)) (ys (list)))
  9. (cond ((null? xs) #f)
  10. ((zero? n) (values (reverse ys) xs))
  11. ((negative? n) #f)
  12. (else (loop (cdr xs) (- n (car xs)) (cons (car xs) ys))))))))
  13.  
  14. (call-with-values
  15. (lambda () (split-equal '(1 2 3 4 5 6 21)))
  16. (lambda (front back) (display front) (newline) (display back) (newline)))
  17. (newline)
  18.  
  19. (call-with-values
  20. (lambda () (split-equal '(1 90 50 30 5 3 2 1)))
  21. (lambda (front back) (display front) (newline) (display back) (newline)))
  22. (newline)
  23.  
  24. (display (split-equal '(50 100 1000))) (newline)
Success #stdin #stdout 0.03s 8220KB
stdin
Standard input is empty
stdout
(1 2 3 4 5 6)
(21)

(1 90)
(50 30 5 3 2 1)

#f