slack1
2017-12-14 18:31:46

Is there a version of fold where the lambda is passed a copy of the list as well as the index position of the current item?


slack1
2017-12-14 18:32:00

Or is that better done as a general recursive function?


slack1
2017-12-14 18:32:09

(basically the reduce from JS)


lexi.lambda
2017-12-14 18:32:43

@slack1 for the index of the list, you can use sequence-fold or for/fold with in-indexed. for the copy of the list part… I’m not sure why you would want that, since the list will be in scope where you call foldl, since lambdas are closures.


lexi.lambda
2017-12-14 18:33:56

so if your list is called lst, you can just write (foldl (lambda (x acc) #\| lst is in-scope in here \|#) init lst)


slack1
2017-12-14 18:34:29

ah I see, thanks!


slack1
2017-12-14 18:34:45

my motivation was a compare-multiple-sequential-items problem


lexi.lambda
2017-12-14 18:35:13

you probably don’t want to use the index for that. lists are not random-access, so getting an element by index is O(n)


lexi.lambda
2017-12-14 18:36:25

maybe it would make sense to use some sort of sliding window sequence instead?


slack1
2017-12-14 18:42:01

oh I see, vector has no fold equivilent


lexi.lambda
2017-12-14 18:42:40

you could use in-vector with for/fold or sequence-fold


slack1
2017-12-14 18:43:48

ah thanks for the directions


slack1
2017-12-14 18:43:55

looking into them slowly…