From dacfce1d39c23b628343ba978260fe94e7416b74 Mon Sep 17 00:00:00 2001 From: walking-octopus Date: Thu, 17 Apr 2025 21:31:26 +0300 Subject: [PATCH] fix: optimize sliding --- main.rkt | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/main.rkt b/main.rkt index 6904088..6ce8457 100644 --- a/main.rkt +++ b/main.rkt @@ -102,16 +102,17 @@ (define (sliding lst size [step 1]) - (define (tail-call lst) - (if (>= size (length lst)) - (list lst) - (cons (take lst size) - (tail-call (drop lst step))))) - (cond - [(> step (length lst)) - (error "step has to be equal to or smaller than length of the list")] - [(= step (length lst)) (list lst)] - [else (tail-call lst)])) + (cond [(> step (length lst)) + (error "step has to be equal to or smaller than length of the list")] + [(= step (length lst)) + (list lst)] + [(let recur ([lst lst] + [len (length lst)]) + (if (>= size len) + (if (empty? lst) empty (list lst)) + (cons (take lst size) + (recur (drop lst step) + (- len step)))))])) (define (scanl proc lst)