Skip to content

Commit 198512b

Browse files
authored
Merge pull request #29 from usefulmove/develop
Optimize `scanr` for improved performance
2 parents 3a19caf + 3677e3b commit 198512b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

main.rkt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@
122122
(rest lst)))
123123

124124

125-
; TODO optimize
126125
(define (scanr proc lst)
127-
(foldr
128-
(λ (val acc)
129-
(append (list (proc val (first acc))) acc))
130-
(list (last lst))
131-
(init lst)))
126+
(foldl
127+
(λ (a acc)
128+
(cond ((null? acc) (cons a acc))
129+
(else (cons (proc a (car acc)) acc))))
130+
'()
131+
(reverse lst)))
132132

133133

134134
(define (sorted? lst)

0 commit comments

Comments
 (0)