Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

What is a sunk cost, and how is it related to a decision problem?

Answered: 1 week ago

Question

Answered: 1 week ago

Answered: 1 week ago