add scaling of servings for recipes
This commit is contained in:
parent
ec107926db
commit
a1dc85b2f7
10
html.scm
10
html.scm
|
@ -30,12 +30,14 @@
|
|||
(define recipe-as-html
|
||||
(lambda (recipe)
|
||||
(string-append "<h2>"
|
||||
(title recipe)
|
||||
(recipe-name recipe)
|
||||
"</h2>"
|
||||
"<i>Ingredients</i><br>"
|
||||
(ingredients-as-html (ingredients recipe))
|
||||
"Servings: "
|
||||
(number->string (recipe-servings recipe))
|
||||
"<br><i>Ingredients</i><br>"
|
||||
(ingredients-as-html (recipe-ingredients recipe))
|
||||
"<br><i>Steps</i><br>"
|
||||
(steps-as-html (steps recipe))
|
||||
(steps-as-html (recipe-steps recipe))
|
||||
"<br>")))
|
||||
|
||||
(define pretty-ingredient
|
||||
|
|
91
parse.scm
91
parse.scm
|
@ -1,16 +1,21 @@
|
|||
;; Predicates
|
||||
|
||||
(define title
|
||||
(define recipe-name
|
||||
(lambda (recipe)
|
||||
(car recipe)))
|
||||
(cadr (assoc 'name recipe))))
|
||||
|
||||
(define ingredients
|
||||
(define recipe-ingredients
|
||||
(lambda (recipe)
|
||||
(car (cdr recipe))))
|
||||
(cadr (assoc 'ingredients recipe))))
|
||||
|
||||
(define steps
|
||||
(define recipe-steps
|
||||
(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
|
||||
(lambda (ingredient)
|
||||
|
@ -28,7 +33,28 @@
|
|||
""
|
||||
(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
|
||||
(lambda (ingredients n)
|
||||
|
@ -46,32 +72,43 @@
|
|||
[else (ingredient-by-name (cdr ingredients)
|
||||
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
|
||||
(lambda (recipes search-ingredients)
|
||||
(filter (lambda (x) (contains-ingredients? x search-ingredients))
|
||||
recipes)))
|
||||
|
||||
(define recipe-by-name
|
||||
(lambda (recipes name)
|
||||
(lambda (name recipes)
|
||||
(if (null? recipes)
|
||||
#f
|
||||
(let ((recipe (car recipes)))
|
||||
(if (equal? (title recipe)
|
||||
(if (equal? (recipe-name recipe)
|
||||
name)
|
||||
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))))
|
||||
|
|
Loading…
Reference in New Issue
Block a user