Taking definition of foldl from Expanded form of fold in Racket , is there any advantage of the first over the second function:
(define (oldfoldl f init l)
(let loop ([init init] [l l])
(if (null? l) init
(loop (f (car l) init) (cdr l)))))
(define (myfoldl f init l)
(if (null? l) init
(myfoldl f (f (car l) init) (cdr l)) ))
In other words, are there any advantages of using an inner sub-function? Both give same result:
(oldfoldl + 0 '(1 2 3 4))
(myfoldl + 0 '(1 2 3 4))
Output:
10
10
Edit: a third version which uses define instead of let follows. Will it be different:
(define (myfoldl2 f init l)
(define (loop [init init] [l l])
(if (null? l) init
(loop (f (car l) init) (cdr l)) ))
(loop init l) )