2017-04-05 00:20:53 +00:00
|
|
|
;; Predicates
|
|
|
|
|
|
|
|
(define title
|
|
|
|
(lambda (recipe)
|
2017-04-05 13:02:19 +00:00
|
|
|
(car recipe)))
|
2017-04-05 00:20:53 +00:00
|
|
|
|
|
|
|
(define ingredients
|
|
|
|
(lambda (recipe)
|
|
|
|
(car (cdr recipe))))
|
|
|
|
|
|
|
|
(define steps
|
|
|
|
(lambda (recipe)
|
|
|
|
(car (cdr (cdr recipe)))))
|
|
|
|
|
|
|
|
(define ingredient-name
|
|
|
|
(lambda (ingredient)
|
|
|
|
(if (= (length ingredient) 2)
|
|
|
|
(car (cdr ingredient))
|
|
|
|
(car (cddr ingredient)))))
|
|
|
|
|
|
|
|
(define ingredient-amount
|
|
|
|
(lambda (ingredient)
|
|
|
|
(car ingredient)))
|
|
|
|
|
2017-04-05 13:02:38 +00:00
|
|
|
(define ingredient-unit
|
|
|
|
(lambda (ingredient)
|
|
|
|
(if (= (length ingredient) 2)
|
|
|
|
""
|
|
|
|
(car (cdr ingredient)))))
|
2017-04-05 00:20:53 +00:00
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
(define ingredient-ref
|
|
|
|
(lambda (ingredients n)
|
|
|
|
(if (= n 0)
|
|
|
|
(car ingredients)
|
|
|
|
(ingredient-ref (cdr ingredients) (- n 1)))))
|
|
|
|
|
|
|
|
(define ingredient-by-name
|
|
|
|
(lambda (ingredients search-name)
|
|
|
|
(let ((ingredient (car ingredients)))
|
|
|
|
(cond [(equal? (ingredient-name ingredient)
|
|
|
|
search-name)
|
|
|
|
ingredient]
|
|
|
|
[(null? (cdr ingredients)) #f]
|
|
|
|
[else (ingredient-by-name (cdr ingredients)
|
|
|
|
search-name)]))))
|
|
|
|
|
|
|
|
(define contains-ingredient?
|
2017-04-05 10:19:03 +00:00
|
|
|
(lambda (recipe search-ingredient)
|
2017-04-05 11:28:32 +00:00
|
|
|
(contains-ingredients? recipe (cons search-ingredient '()))))
|
2017-04-05 10:19:03 +00:00
|
|
|
|
|
|
|
(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
|
2017-04-05 11:28:32 +00:00
|
|
|
(lambda (recipes search-ingredients)
|
2017-04-05 13:08:16 +00:00
|
|
|
(filter (lambda (x) (contains-ingredients? x search-ingredients))
|
|
|
|
recipes)))
|
2017-04-05 22:22:37 +00:00
|
|
|
|
|
|
|
(define recipe-by-name
|
|
|
|
(lambda (recipes name)
|
|
|
|
(if (null? recipes)
|
|
|
|
#f
|
|
|
|
(let ((recipe (car recipes)))
|
|
|
|
(if (equal? (title recipe)
|
|
|
|
name)
|
|
|
|
recipe
|
|
|
|
(recipe-by-name (cdr recipes) name))))))
|