Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.31 KB | None | 0 0
  1. (setf dishes '(
  2.     ("a" (("1" 10) ("2" 20) ("3" 10)))
  3.     ("b" (("2" 5) ("4" 5) ("6" 40)))
  4.     ("c" (("3" 3) ("4" 65)))
  5.     ("d" (("1" 20) ("6" 32)))
  6.     )
  7. )
  8.  
  9. (setf prod '(
  10.     ("2" 100)
  11.     ("6" 100)
  12.     ("3" 100)
  13.     ("4" 100)
  14.     )
  15. )
  16.  
  17. (defun and_list (a)
  18.     (cond
  19.         ((null a) t)
  20.         (t (and (car a) (and_list (cdr a))))
  21.     )
  22. )
  23.  
  24. ; have product (p) in product list (pl) -> t | nil
  25. (defun have_prod (p pl)
  26.     (cond
  27.         ((null pl) nil)
  28.         ((and (equal (car p) (caar pl)) (< (cadr p) (cadar pl))) t)
  29.         (t (have_prod p (cdr pl)))
  30.     )
  31. )
  32.  
  33. ; can cook dish (d) with product list (p) -> t | nil
  34. (defun can_cook (d p)
  35.    (and_list (mapcar (lambda (a) (have_prod a p)) (cadr d)))
  36. )
  37.  
  38. ; remove product (p) from product list (pl) -> list(2)
  39. (defun rm_prod (p pl)
  40.     (cond
  41.         ((null pl) nil)
  42.         ((and (equal (car p) (caar pl)) (< (cadr p) (cadar pl))) (cons (list (car p) (- (cadar pl) (cadr p))) (rm_prod p (cdr pl)))) ; mb without AND (right side)
  43.         (t (cons (car pl) (rm_prod p (cdr pl))))
  44.     )
  45. )
  46.  
  47. ; create menu list -> list(1)
  48. (defun menu (d p)
  49.     (cond
  50.         ((null d) nil)
  51.         ((null p) nil)
  52.         ((can_cook (car d) p) (cons (caar d) (menu (cdr d) (rm_prod (car d) p))))
  53.         (t (menu (cdr d) p))
  54.     )
  55. )
  56.  
  57. (print (menu dishes prod))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement