Question: OCAML Lets have cake! We will model a cupcake by its price, weight, calories and list of ingredients as per the prelude code. Some examples

OCAML Lets have cake!

We will model a cupcake by its price, weight, calories and list of ingredients as per the prelude code. Some examples of cupcakes are also provided.

Your task is to implement a function allergy_free : ingredient list -> cupcake list -> cupcake list. It takes a list of ingredients allergens and a list of cupcakes cupcakes as input, and returns the cupcakes from the list that do not contain any of the listed allergens. Cupcakes in the returned list should appear in the same order that they did in the input list. Note that none of the ingredient lists are sorted.

allergy_free [Nuts; Gluten] cupcakes;; - : cupcake list = [Cupcake (2.75, 90.5, 275, [Dairy; Soy])]

In order to get full marks, you must use each of the following higher-order functions:

List.filter : ('a -> bool) -> 'a list -> 'a list

List.exists : ('a -> bool) -> 'a list -> bool

List.for_all : ('a -> bool) -> 'a list -> bool Prelude:

 type price = float type weight = float type calories = int type ingredient = Nuts | Gluten | Soy | Dairy type cupcake = Cupcake of price * weight * calories * ingredient list let c1 = Cupcake (2.5, 80.3, 250, [Dairy; Nuts]) let c2 = Cupcake (2.75, 90.5, 275, [Dairy; Soy]) let c3 = Cupcake (3.05, 100.4, 303, [Dairy; Gluten; Nuts]) let c4 = Cupcake (3.25, 120.4, 330, [Dairy; Gluten ]) let cupcakes = [c1 ; c2 ; c3 ; c4]

(* -------------------------------------------------------------*)

(* QUESTION 1 : Let's have cake! *)

(* -------------------------------------------------------------*)

(* allergy_free : ingredient list -> cupcake list -> cupcake list *)

let allergy_free allergens cupcakes =

(* your answer here*)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!