add scaling of servings for recipes

This commit is contained in:
Christoffer Müller Madsen 2017-04-08 20:33:00 +02:00
parent ec107926db
commit a1dc85b2f7
2 changed files with 70 additions and 31 deletions

View File

@ -30,12 +30,14 @@
(define recipe-as-html (define recipe-as-html
(lambda (recipe) (lambda (recipe)
(string-append "<h2>" (string-append "<h2>"
(title recipe) (recipe-name recipe)
"</h2>" "</h2>"
"<i>Ingredients</i><br>" "Servings: "
(ingredients-as-html (ingredients recipe)) (number->string (recipe-servings recipe))
"<br><i>Ingredients</i><br>"
(ingredients-as-html (recipe-ingredients recipe))
"<br><i>Steps</i><br>" "<br><i>Steps</i><br>"
(steps-as-html (steps recipe)) (steps-as-html (recipe-steps recipe))
"<br>"))) "<br>")))
(define pretty-ingredient (define pretty-ingredient

View File

@ -1,16 +1,21 @@
;; Predicates (define recipe-name
(define title
(lambda (recipe) (lambda (recipe)
(car recipe))) (cadr (assoc 'name recipe))))
(define ingredients (define recipe-ingredients
(lambda (recipe) (lambda (recipe)
(car (cdr recipe)))) (cadr (assoc 'ingredients recipe))))
(define steps (define recipe-steps
(lambda (recipe) (lambda (recipe)
(car (cdr (cdr recipe))))) (cadr (assoc 'steps recipe))))
(define recipe-servings
(lambda (recipe)
(if (recipe-specifies-servings? recipe)
(cadr (assoc 'servings recipe))
1)
))
(define ingredient-name (define ingredient-name
(lambda (ingredient) (lambda (ingredient)
@ -28,7 +33,28 @@
"" ""
(car (cdr ingredient))))) (car (cdr ingredient)))))
;; ;; Predicates
(define contains-ingredient?
(lambda (recipe search-ingredient)
(contains-ingredients? recipe (cons search-ingredient '()))))
(define contains-ingredients?
(lambda (recipe search-ingredients)
(let ((ingredients (recipe-ingredients recipe)))
(cond [(null? search-ingredients) #t]
[(ingredient-by-name ingredients
(car search-ingredients))
(contains-ingredients? recipe
(cdr search-ingredients))]
[else #f])
)))
(define recipe-specifies-servings?
(lambda (recipe)
(assoc 'servings recipe)))
;; Searching and indexing
(define ingredient-ref (define ingredient-ref
(lambda (ingredients n) (lambda (ingredients n)
@ -46,32 +72,43 @@
[else (ingredient-by-name (cdr ingredients) [else (ingredient-by-name (cdr ingredients)
search-name)])))) search-name)]))))
(define contains-ingredient?
(lambda (recipe search-ingredient)
(contains-ingredients? recipe (cons 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 (define recipes-by-ingredients
(lambda (recipes search-ingredients) (lambda (recipes search-ingredients)
(filter (lambda (x) (contains-ingredients? x search-ingredients)) (filter (lambda (x) (contains-ingredients? x search-ingredients))
recipes))) recipes)))
(define recipe-by-name (define recipe-by-name
(lambda (recipes name) (lambda (name recipes)
(if (null? recipes) (if (null? recipes)
#f #f
(let ((recipe (car recipes))) (let ((recipe (car recipes)))
(if (equal? (title recipe) (if (equal? (recipe-name recipe)
name) name)
recipe recipe
(recipe-by-name (cdr recipes) name)))))) (recipe-by-name name (cdr recipes)))))))
;; Manipulation
(define scale-recipe
(lambda (recipe wanted-servings)
(let ([name (recipe-name recipe)]
[ingredients (recipe-ingredients recipe)]
[steps (recipe-steps recipe)]
[servings (recipe-servings recipe)])
(let ([new-name name]
[new-ingredients (map (lambda (ingr)
(list (* (/ (car ingr)
servings)
wanted-servings)
(cdr ingr)))
ingredients)]
[new-steps steps]
[new-servings wanted-servings])
(make-recipe new-name new-servings new-ingredients new-steps)
))))
;; Constructors
(define make-recipe
(lambda (name servings ingredients steps)
`((name ,name) (servings ,servings) (ingredients ,ingredients) (steps ,steps))))