; split an array
(define (sum xs) (apply + xs))
(define (split-equal xs)
(let ((n (sum xs)))
(if (odd? n) #f
(let loop ((xs xs) (n (/ n 2)) (ys (list)))
(cond ((null? xs) #f)
((zero? n) (values (reverse ys) xs))
((negative? n) #f)
(else (loop (cdr xs) (- n (car xs)) (cons (car xs) ys))))))))
(call-with-values
(lambda () (split-equal '(1 2 3 4 5 6 21)))
(lambda (front back) (display front) (newline) (display back) (newline)))
(newline)
(call-with-values
(lambda () (split-equal '(1 90 50 30 5 3 2 1)))
(lambda (front back) (display front) (newline) (display back) (newline)))
(newline)
(display (split-equal '(50 100 1000))) (newline)
OyBzcGxpdCBhbiBhcnJheQoKKGRlZmluZSAoc3VtIHhzKSAoYXBwbHkgKyB4cykpCgooZGVmaW5lIChzcGxpdC1lcXVhbCB4cykKICAobGV0ICgobiAoc3VtIHhzKSkpCiAgICAoaWYgKG9kZD8gbikgI2YKICAgICAgKGxldCBsb29wICgoeHMgeHMpIChuICgvIG4gMikpICh5cyAobGlzdCkpKQogICAgICAgIChjb25kICgobnVsbD8geHMpICNmKQogICAgICAgICAgICAgICgoemVybz8gbikgKHZhbHVlcyAocmV2ZXJzZSB5cykgeHMpKQogICAgICAgICAgICAgICgobmVnYXRpdmU/IG4pICNmKQogICAgICAgICAgICAgIChlbHNlIChsb29wIChjZHIgeHMpICgtIG4gKGNhciB4cykpIChjb25zIChjYXIgeHMpIHlzKSkpKSkpKSkKCihjYWxsLXdpdGgtdmFsdWVzCgkobGFtYmRhICgpIChzcGxpdC1lcXVhbCAnKDEgMiAzIDQgNSA2IDIxKSkpCgkobGFtYmRhIChmcm9udCBiYWNrKSAoZGlzcGxheSBmcm9udCkgKG5ld2xpbmUpIChkaXNwbGF5IGJhY2spIChuZXdsaW5lKSkpCihuZXdsaW5lKQoKKGNhbGwtd2l0aC12YWx1ZXMKCShsYW1iZGEgKCkgKHNwbGl0LWVxdWFsICcoMSA5MCA1MCAzMCA1IDMgMiAxKSkpCgkobGFtYmRhIChmcm9udCBiYWNrKSAoZGlzcGxheSBmcm9udCkgKG5ld2xpbmUpIChkaXNwbGF5IGJhY2spIChuZXdsaW5lKSkpCihuZXdsaW5lKQoKKGRpc3BsYXkgKHNwbGl0LWVxdWFsICcoNTAgMTAwIDEwMDApKSkgKG5ld2xpbmUp