From c2907889704382ef73bf0c89248b58969dcff5e6 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Sat, 31 Jul 2021 16:31:28 +0300 Subject: [PATCH 1/2] Refactor remove-element-from-list --- recipes/remove-element-from-list.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/recipes/remove-element-from-list.md b/recipes/remove-element-from-list.md index 1e5f531..34ed13c 100644 --- a/recipes/remove-element-from-list.md +++ b/recipes/remove-element-from-list.md @@ -13,9 +13,8 @@ given predicate procedure. (if (null? list) (reverse result) (let ((item (car list))) - (if (fn item) - (iter (cdr list) result) - (iter (cdr list) (cons item result))))))) + (iter (cdr list) + (if (fn item) result (cons item result))))))) ``` Credit [Jakub T. Jankiewicz](https://jcubic.pl/me) From 2c05b9f2bf26e432d5a85378f9a8dc6db64b4743 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Sat, 31 Jul 2021 16:32:38 +0300 Subject: [PATCH 2/2] Use naming conventions (#16) list -> lst iter -> loop --- recipes/remove-element-from-list.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/recipes/remove-element-from-list.md b/recipes/remove-element-from-list.md index 34ed13c..d2b73bd 100644 --- a/recipes/remove-element-from-list.md +++ b/recipes/remove-element-from-list.md @@ -8,12 +8,12 @@ given predicate procedure. ## Solution ```Scheme -(define (remove fn list) - (let iter ((list list) (result '())) - (if (null? list) +(define (remove fn lst) + (let loop ((lst lst) (result '())) + (if (null? lst) (reverse result) - (let ((item (car list))) - (iter (cdr list) + (let ((item (car lst))) + (loop (cdr lst) (if (fn item) result (cons item result))))))) ``` Credit [Jakub T. Jankiewicz](https://jcubic.pl/me)