SML Exercises These are a sample of relatively simple SML exercies for practice. Answers are found in the file answers.txt. 1) What is the type of the function? fun identity(x) = x; 2) Make up a value of the type bool_or_int. datatype bool_or_int = Hot of bool | Cold of int; 3) a) What is the type of hot_maker? fun hot_maker(x) = Hot; b) What is the type of Hot? 4) Define a recursive function sum that adds up all the integers from 1 to a given upper limit: sum : int -> int (e.g. sum 5 = 15). 5) Explain the system response to the following: fun x _ = 3 and val y = x; 6) Define a function intmax which takes an integer pair and returns the greater of its two components. 7) a) Write the function power_of_two that tests if an int is a power of 2. b) Write each steps of the evaluation of (power_of_two 8) 8) Write two functions odd and even that define if an int is even or odd using mutual recursion. 9) What are the results of the four let expressions in the following sequence (entered successively in the interactive top level)? val x = 1 and y = 2 and z = 3; let val x = x+1 and z = x+4 in x + z end; let val x = x+1 in let val z = x+4 in x + z end end; let val x = x+1 val z = x+4 in x + z end; let val y = x+1 in let val x = x+1 in x end end; 10) Write a function insert that inserts an int int a sorted list of ints so that the result remains sorted (in ascending order). 11) Write a function insertion_sort: int list -> int list. The argument is an arbitrary list of integers, and the result should be a sorted version of that list. 12) Write a function that computes the set of all subsets of a set. How would you represent a set? 13) What is the type of C? fun C f g x = f (g x); 14) a) What is the type of this function? fun newfunc F y nil = y | newfunc F y (x::l) = F(x, (newfunc F y l)); b) What library list function is it equivalent to? 15) a) Write a datatype COORDS that defines coordinates of a point in 3D space. b) Give some examples of COORDS. c) Write a function distance that computes the distance between two COORDS. d) Give examples of the use of distance. 16) Create a datatype PERSON that defines a person by his name, fname, age, and dob. 17) a) Define a reference variable i whose value is a reference to 10. b) Increment the value of i by 1. c) Decrement the value of i by 1. d) Change the value of i to 20. 18) Write the fibonacci function, fib : int -> int, such that fib n (n >= 0) is the nth element in the fibonacci sequence: 1, 1, 2, 3, 5, 8, ... (don't care about fib n for negative n). 19) Write a function sign that determines whether a number is positive (return 1), negative (return -1), or 0 (return 0). 20) Write a function that computes the cube of an int. 21) a) Write a function that returns the sum of all odd elements in a list of integers. b) Write a function that returns the sum of all elements satisfying the predicate p in a list of integers. c) Write a function that returns the sum of all elements NOT satisfying the predicate p in a list of integers. 22) In a cafe, menu items are represented by the following datatype: datatype item = Vgn of string * real | Vgt of string * real | Omn of string * real The constructors are Vgn == vegan == no animal produce Vgt == vegetarian == no meat Omn == omnivorous == may contain animal produce The string is the item's name and the real is it's price. A menu is represented as a list of items, for example: [Vgn("tofu",3.6), Vgt("quiche",2.7), Omn("haggis",1.9)]; a) Write a function to find all vegan items on a list. What is the type of the function? b) Write a function to find the cheapest omnivorous item on the list. What is the type of the function? An order is a tuple of a food item and a quantity, for example: ("tofu",3) == 3 orders of tofu. c) Write a function which looks up an item and returns the price. What is the type of this function? d) Write a function which, given an order tuple and a menu, returns the total cost of the order. What is the type of this function? e) Write a function which, given a list of order tuples and a menu, returns the total cost of all the orders. What is the type of the function?