(* F# Assignment 0. Simple Drills. It's recommend that you do this problem inside the interactive interpreter. This problem will not be graded. a. use let to bind variables and functions. Note: when using let inside a function definition, the syntax is let x= ... in ... You may have to by trial and error the different syntactic restrictions of F# b. bind and assign to mutable variables. c. use for loops. d. define a type as follows: type direction = North | South | East | West;; Write a function that uses pattern matching, and which returns 0 for North, 1 for South, 2 for East and 3 for West. e. Use tuples, lists and arrays. Note the difference between , and ;. For an array a, a.Length is the length - it's .Net compatible. Arrays are mutable but lists and tuples are not. Please don't try to use F# like C# or other conventional programming languages. Try to use its unique characteristics. For this assignment, download the fractions.dll file and add your code to the numbers.fs file. 1. Re-write the gcd function in F#. Use PATTERN MATCHING. That is, instead of the if-else, use two cases. You can find the C# version of tail-recursive gcd in fractions.cs. 2. Complete the function that adds two arbitrary reals as described in the lecture. Note: do not convert everything into floats. 1/3 is a more accurate representation of the rational number than 0.333333, which has only finite precision. Your program must preserve the *same level of precision*: if you're adding a float and a rational it's ok to produce a float, but not when you're adding two rationals. 3. Write a function that returns the *sum* of all reals in a rlist. The type of the function should be rseq -> real. I(0) is the sum of the empty list. 4. Write a function equals: real*real -> bool. That tests if two real numbers are "equal". For example, equals(I(3),R(rational(6,2))) should return true (because 6/2=3). An integer like 2 should be equated with 2.0, but NOT 1.9999 or 2.0001 (equality needs to be transitive). A rational n/d can be equated with a float f if float(n)/float(d)=f. Additional hints: You can access the numerator/denominator of a rational object directly since they're declared public. To convert an int x into a float, use float(x). Given float y, int(y) will return the whole number part and y%1.0 will return the decimal part. Additional warning: do not try to use a pattern with repeat variables. : | (x,x) -> ... won't be accepted. Instead, use | (x,y) when x=y -> ... One language that does allow repeat patterns is called Prolog, but that's not really a general-purpose programming language. 5. Write a function "find" so that find(x,m) will return true if x is "equals" to some element in m, and false otherwise. Hint: ML does a good job of inferring the type of functions, but sometimes you'll need to give it a hint. For example: let f (x:int) = Console.WriteLine(string(x));; The ":int" hints to ML that the x parameter is an integer (the ()'s are necessary too). The above definition for f can also written as let f = fun (x:int) -> Console.WriteLine(string(x));; and as let f = function | (x:int) -> Console.WriteLine(string(x));; Don't forget to use 'let rec' when defining recursive functions. 6. Redo problems 3 and 5 for the mutable rlist class (see numbers15.fs). GENERAL HINTS: You may experience some frustration dealing with F# syntax requirements. You'll need to experiment a bit to devise some syntactic conventions that work for you. The interactive interpreter is a great help for this. To a lot of programmers, when presented with a new programming language, they will always try to use it in the same way as the language they're most familiar with. Although that's not entirely unreasonable, it's also the reason why a lot of people find it difficult to adjust to new languages. For example, when a C programmer is presented with Java, such a programmer will try to use Java like C. OF COURSE you will absolutely hate Java if you try to use it like C. What's the point of learning a new language if that's how you're going to use it? You cannot be afraid to test the new features of F# - of what makes it unique. I've just given you the keys to a Ferrari: don't drive like an old truck. *)