From ba8dc209644fc97f2a74d1986a27cdde06d824e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Wed, 5 Apr 2017 13:28:32 +0200 Subject: [PATCH] reduced code duplication in recipes-by-ingredients and contains-ingredients? --- parse.scm | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/parse.scm b/parse.scm index 3f964c0..e1aa7ba 100644 --- a/parse.scm +++ b/parse.scm @@ -43,8 +43,7 @@ (define contains-ingredient? (lambda (recipe search-ingredient) - (let ((ingredients (ingredients recipe))) - (ingredient-by-name ingredients search-ingredient)))) + (contains-ingredients? recipe (cons search-ingredient '())))) (define contains-ingredients? (lambda (recipe search-ingredients) @@ -58,17 +57,20 @@ ))) (define recipes-by-ingredients + (lambda (recipes search-ingredients) + (recipes-by-ingredients-rec recipes search-ingredients '()))) + +(define recipes-by-ingredients-rec (lambda (recipes search-ingredients list) (if (null? recipes) list (let* ((recipe (car recipes)) (ingredients (ingredients recipe)) (contains? (contains-ingredients? recipe - search-ingredients))) - (if contains? - (recipes-by-ingredients (cdr recipes) + search-ingredients)) + (new-list (if contains? + (cons recipe list) + list))) + (recipes-by-ingredients-rec (cdr recipes) search-ingredients - (cons recipe list)) - (recipes-by-ingredients (cdr recipes) - search-ingredients - list)))))) + new-list)))))