datatype tree = Oak | Elm | Maple | Spruce | Fir | Pine | Willow; datatype vegetable = Carrot | Zucchini | Tomato | Cucumber | Lettuce; datatype grain = Wheat | Oat | Barley | Maize; datatype plot = Grove of tree | Garden of vegetable | Field of grain | Vacant; datatype bread = White | MultiGrain | Rye | Kaiser; datatype spread = Mayo | Mustard; datatype vegetable = Cucumber | Lettuce | Tomato; datatype deliMeat = Ham | Turkey | RoastBeef | ExtraVeg of vegetable ; datatype noodle = Spaghetti | Penne | Fusilli | Gemelli | Farfalle; datatype sauce = Pesto | Marinara | Creamy; datatype protein = MeatBalls | Sausage | Chicken | Tofu; datatype entree = Sandwich of bread * spread * vegetable * deliMeat | Pasta of noodle * sauce * protein; datatype salad = Caesar | Garden; datatype side = Fries | Chips | CarrotSticks | GarlicBread | Salad of salad; datatype beverage = Water | Coffee | Pop | Lemonade | IceTea; datatype meal = Meal of entree * side * beverage; datatype number = Int of int | Real of real; (* interpret the first int as numerator and second as denominator *) datatype rational = Fraction of int * int; (* 1.10.1 *) (* A HW problem; this is just to get you started *) datatype book = (* author, title, pages *) Novel of string * string * int (* title, number of vols *) | RefBook of string * int (* author, title, subject, pages *) | NonFiction of string * string * string * int ... fun fahrToCel(t) = (t - 32) * 5 div 9; fun f(x) = 3.0 * x*x - 2.7 * x + 14.4; fun capitalize(s) = str(Char.toUpper(String.sub(s, 0))) ^ String.substring(s, 1, size(s) - 1); fun f(1) = 0 | f(x) = (x * x - 1) div (x - 1); fun plotYield(Garden(Carrot)) = 100 | plotYield(Garden(Zucchini)) = 3 | plotYield(Garden(Tomato)) = 1000 | plotYield(Garden(Cucumber)) = 1200 | plotYield(Garden(Lettuce)) = 800 | plotYield(Field(Maize)) = 10000 | plotYield(Field(x)) = 5000 | plotYield(x) = 0; (* 1.11.5 *) fun replaceFries(Meal(x, Fries, y)) = Meal(x, Chips, y) | replaceFries(Meal(x, z, y)) = Meal(x, z, y); (* 1.11.3 *) fun add(Int(x), Int(y)) = Int(x + y) | add(Real(x), Real(y)) = Real(x + y) | add(Int(x), Real(y)) = Real(real(x) + y) | add(Real(x), Int(y)) = Real(x + real(y)); fun add(Fraction(a, b), Fraction(c, d)) = Fraction(a * d + c * b, b * d); fun gcd(a, 0) = a | gcd(a, b) = gcd(b, a mod b); fun add(Fraction(a, b) , Fraction(c, d)) = Fraction((a * d + c * b) div gcd(a * d + c * b, b * d)), (b * d) div gcd(a * d + c * b, b * d)); fun add(Fraction(a, b), Fraction(c, d)) = let val numerator = a * d + c * b; val denominator = b * d; val divisor = gcd(numerator, denominator); in Fraction(numerator div divisor, denominator div divisor) end;