CSC 123/252 F#/ML and .Net Integration Lab Part I: Basic F# drills. The syntax of F# is not as easy to get used to as Ruby or Python. Experiment with using each of the following constructs WITHIN the interactive console. 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. f. In most modern languages including C# and Ruby, when an array is passed to a function, as in void f(int[] a) in C#, implicitly only a pointer is passed, so that if the function changes the content of a, it is not just a local change. Devise a simple experiment to determine if F# behaves in the same way. //////////////////////////////////////////////////////////////////////// Part II. For this lab you should download fractions.cs and numlist.fs fractions.cs is written in C# and contains a basic class for fractions. Study it first so you'll understand how the fraction class can be used. Compile it into a .dll with csc /t:library fractions.cs We will now use this .dll in a F# program. We can create an instance of fraction in ml with expressions such as fraction(1,3). ML/F# optimizes tail recursion like Scheme, but also has a for loop (see bubblesort example in misc.fs) But for this assignment, you should try to use recursion. 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. Study the numlist.fs program to see how it works. Experiment with calling the C# objects from F#: use "let" to bind variables to some fractions such as 1/2, 2/5, etc... Call each of the functions in the fractions class to see how it works. If you evaluate an expression but don't use let to bind it to a variable, it is bound by default to "it"; "it" is like the _ variable in Perl 3. Write a function 'length' that returns the length of a list of numbers. DO NOT USE IF-ELSE. Use pattern matching. 4. Write a function to determine if two numbers are equal. You'll need 4 pattern-matching cases, int-int, rat-rat, int-rat, rat-int. 5. Write a function to add up all the elements of a list of numbers. The function should return a fraction or a "number". Hint: convert integers n into fractions: fraction(n,1). 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.