all but 2nd-to-last atom added by sethalves on Sat Sep 13 06:29:06 2014

(define (atom? x) (not (pair? x)))

(define (butSecondLastAtom* lst seen total)
  (cond ((null? lst) '())
        ((atom? lst) lst)
        ((null? (car lst))
         (cons '() (butSecondLastAtom* (cdr lst) seen total)))
        ((atom? (car lst))
         (vector-set! seen 0 (+ (vector-ref seen 0) 1))
         (if (= (- total (vector-ref seen 0)) 1)
             (butSecondLastAtom* (cdr lst) seen total)
           (cons (car lst) (butSecondLastAtom* (cdr lst) seen total))))
        (else
         (let* ((a (butSecondLastAtom* (car lst) seen total))
                (rst (butSecondLastAtom* (cdr lst) seen total)))
           (cons a rst)))))

(define (butSecondLastAtom lst)
  (let ((total (length (flatten lst)))
        (seen (make-vector 1 0)))
    (butSecondLastAtom* lst seen total)))