diff --git a/gnucash/report/report-system/html-document.scm b/gnucash/report/report-system/html-document.scm
index baf0c6aecb..a9c6b162d0 100644
--- a/gnucash/report/report-system/html-document.scm
+++ b/gnucash/report/report-system/html-document.scm
@@ -107,7 +107,8 @@
(define (gnc:html-document-tree-collapse . tree)
(let lp ((e tree) (accum '()))
- (cond ((list? e) (fold lp accum e))
+ (cond ((null? e) accum)
+ ((pair? e) (fold lp accum e))
((string? e) (cons e accum))
(else (cons (object->string e) accum)))))
diff --git a/libgnucash/scm/utilities.scm b/libgnucash/scm/utilities.scm
index 4bdc61ed85..4fcf60b823 100644
--- a/libgnucash/scm/utilities.scm
+++ b/libgnucash/scm/utilities.scm
@@ -212,14 +212,14 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; flattens an arbitrary deep nested list into simple list. this is
;; probably the most efficient algorithm available. '(1 2 (3 4)) -->
-;; '(1 2 3 4)
+;; '(1 2 3 4) thanks to manumanumanu on #guile
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (gnc:list-flatten . lst)
- (reverse
- (let lp ((e lst) (accum '()))
- (if (list? e)
- (fold lp accum e)
- (cons e accum)))))
+ (let loop ((lst lst) (acc '()))
+ (cond
+ ((null? lst) acc)
+ ((pair? lst) (loop (car lst) (loop (cdr lst) acc)))
+ (else (cons lst acc)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; compatibility hack for fixing guile-2.0 string handling. this code