(* Exercise 1 *) (* 1a. Make it so that that x equals 42, by adding 22 to 20 *) (* let x = ;; *) (* 1b. Make it so that x1 equals 42.0, by adding 2 numbers. *) (* let x1 = (* Your code here. *) ;; *) (* 1c. Write a function takes a string, and appends * ", and that is why I love cos326" to the end of it. *) (* let cos326_loveifier (input:string) = (* Your code here *) ;; *) (* call your loveifier, creating the mylove string -- yes, this is cheesy!: *) (* let mylove:string = ... *) (* 1d. Write a function that takes a number and returns * the difference between that number and 42. * Eg, if 'num' is 50, the result should be 8. * If 'num' is 30, the result should be -12 *) (* let difference_between_num_and_42 num = (* Your code here *) ;; *) (* 1e. One more simple arithmetic example... * Write a function that returns the volume of a cylinder * with height h, radius r. (volume is pi * r^2 * h) *) (* let volume_cylinder (h:float) (r:float) : float = (* Your code here *) ;; *) (* 1f. Determine if an integer is even. *) (* let even (x: int) : bool = (* Your code here *) ;; *) (* 1g. Write odd in terms of even *) (* let odd (x: int) : bool = (* Your code here *) ;; *) (* 1h. OCaml comes pre-packaged with a standard library, that includes * a lot of utility functions that you don't have to write yourself. * For instance, check out the String module * (http://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html) * * Now... write a function that takes a String, and returns whether * or not that String is more than 10 characters long. *) (* let is_more_than_10_characters_long str : bool = (* Your code here. *) ;; *) (* Exercise 2 *) (* 2. What does the following expression evaluate to? Why?*) (* 1.0 == 1.0;; *) (* Moral of the story : Don't use == unless you know what this is for *) (* 3. Compute the GCD for two integers using Euclid's recursion * http://en.wikipedia.org/wiki/Greatest_common_divisor *) (* let gcd (x : int) (y : int) : int = *) (* 4. Compute the McCarthy 91 function as shown in * http://en.wikipedia.org/wiki/McCarthy_91_function *) (* let mccarthy (x : int) : int = failwith "Unimplemented" *) (* 5. Compute the square root of x using Heron of Alexandria's * algorithm (circa 100 AD). x must be greater than 1.0. * We start with an initial (poor) guess that the square root is 1.0. * Let's call our current guess g. We'll maintain the invariant that * g^2 is less than x and therefore that g is less than the square root * of x. * Notice that if g is less than the square root of x then x/g is slightly * greater than the square root of x. The real square root is then between * g and x/g. * To compute a slightly better guess than g, we can take the average of * g and x/g: g + x/g ------- 2 * We can keep improving our guess by averaging again and again. Stop * the process when you get pretty close. ie: the difference between * g and x/g is small, perhaps less than 0.001. * More on this method here: * http://www.mathpages.com/home/kmath190.htm * More on Heron of Alexandria here: * http://en.wikipedia.org/wiki/Hero_of_Alexandria *) (* let squareRoot (x : float) : float = *)