(* --- In-class example --- *) datatype icecream = VanillaIC | ChocolateIC | StrawberryIC | MintIC| AnchovieIC; datatype syrup = ChocolateS | CaramelS | StrawberryS | HotFudge | TartarSauceS ; datatype topping = RainbowSprinkles | ChocolateSprinkles | ChocolateChips | Walnuts | Pecans | Cherry | Blueberries; datatype cone = CakeC | SugarC | WaffleC; datatype donutDough = CakeD | YeastD; datatype donutFlavor = VanillaD | ChocolateD | BlueberryD | PumpkinD | CinnamonD; datatype frostingFlavor = VanillaF | ChocolateF | StraberryF | MapleF; datatype donutEncasement = Plain | Frosting of frostingFlavor | Glaze | Powder; datatype pop = RootBeer | Cola | CreamSoda | DrSalt; datatype treat = IcecreamCone of icecream * cone | IcecreamSundae of icecream * syrup * topping | Donut of donutDough * donutFlavor * donutEncasement | Float of pop * icecream; (* --- from the book --- *) 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 (* author, title, subject, publisher *) | Textbook of string * string * string * string ... datatype libraryItem = Book of book | Recording of recording (* Simple function examples *) 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); (* Pattern matching *) fun f(1) = 0 | f(x) = (x * x - 1) div (x - 1); fun asciiToppings(ChocolateChips) = "^ ^ ^ ^" | asciiToppings(Walnuts) = "(*#*)" | asciiToppings(Pecans) = "(||)" | asciiToppings(Cherry) = "o'" | asciiToppings(Blueberries) = "bbbbb" | asciiToppings(x) = "..|._.`"; fun outOfRainbowSprinkles(IcecreamSundae(x, y, RainbowSprinkles)) = IcecreamSundae(x, y, ChocolateSprinkles) | outOfRainbowSprinkles(somethingelse) = somethingelse; (* 1.11.3 --- for the number datatype *) 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)); (* For the rational datatype *) 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; fun mul(Fraction(a, b), Fraction(c, d)) = let val numerator = a * c; val denominator = b * d; val divisor = gcd(numerator, denominator); in Fraction(numerator div divisor, denominator div divisor) end;