From ba43937232c267dc42c198f07337a18c9717b892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Wed, 5 Apr 2017 12:19:03 +0200 Subject: [PATCH] add search for recipes by list of ingredients --- parse.scm | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/parse.scm b/parse.scm index f36e89b..3f964c0 100644 --- a/parse.scm +++ b/parse.scm @@ -42,6 +42,33 @@ search-name)])))) (define contains-ingredient? - (lambda (recipe ingredient) + (lambda (recipe search-ingredient) (let ((ingredients (ingredients recipe))) - (ingredient-by-name ingredients ingredient)))) + (ingredient-by-name ingredients search-ingredient)))) + +(define contains-ingredients? + (lambda (recipe search-ingredients) + (let ((ingredients (ingredients recipe))) + (cond [(null? search-ingredients) #t] + [(ingredient-by-name ingredients + (car search-ingredients)) + (contains-ingredients? recipe + (cdr search-ingredients))] + [else #f]) + ))) + +(define recipes-by-ingredients + (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 + (cons recipe list)) + (recipes-by-ingredients (cdr recipes) + search-ingredients + list))))))