## C311 Test 1

### T/F

1. __ F# is statically typed

2. __ Tuples are heterogeneously typed and can contain other tuples

3. __ `[[1; 4]; [-1; 5]; [3; 4]; 5]` is a valid F# list

4. __ `let f x = (fun y → x * y);;` is a curried function

5. __ A function with one function parameter that returns a function is order 2

6. __ `let f5 a b = (a+1)::b;;` can only take integers as an argument for `a`

7. __ The `reduce` function we have written is the same as the `List.foldBack` function.

8. __ The `List.fold` function is the same as `List.foldBack` for associative operations such as `+` and `*`

### Matching (non-exhaustive)

9. __ `(fun a b → a*b)` a. `let f a b = a*b`

10. __ `(fun b → 4*b)`

b. `let f a = (fun b) → a*a`

11. __ `[2; 3; 4]`

c. `let f a b = a * b;; f 4;;`

12. __ `12`

d. `List.foldBack (fun a b → a*b) [3;1;4] 1`

13. __ `10`

e. `List.map (fun a → a+1) [1;2;3]`

f. `List.foldBack (fun x c → x+c) (List.map (fun a → a+1) [1;2;3]) 1`

Using the following definitions:

``````let rec map f L =
match L with
| [] -> []
| h::t -> f h::map f t;;

let rec filter f L =
match L with
| [] -> []
| h::t when f h -> h::filter f t
| h::t -> filter f t;;

let rec reduce f a L =
match L with
| [] -> a
| h::t -> f h (reduce f a t);;

let rec map2 f L1 L2 =
match (L1, L2) with
| ([], []) -> []
| (h1::t1, h2::t2) -> (f h1 h2)::map2 f t1 t2;;

type IS = I of int | S of string;;

let m a b =
match (a,b) with
| (I x, I y) -> I (x*y)
| (I x, S y) -> S y;;

let g x y = (x, y)

let h x y = y @ [x]``````

Give the results of the following. Some may be be errors.

 14. `g 2 "b"` 15. `let f a = g a 8;; f "a";;` 16. `h [3;2] 1` 17. `map (fun a → a+1) [1;2;3;4]` 18. `map2 g [1;2] ["a";"b"]` 19. `map2 (fun a b → (a,b)) [1;2] ["a";"b"]` 20. `reduce h [] [1;2;3]` 21. `filter (fun x → x > 3) [1;2;3;4;5]` 22. `m (I 5) (S "a")` 23. `m (I 5) (I 6)` 24. `m (S "a") (I 5)` 25. Domain of g 26. Range of g

### Programming in F#

1. Define a function `count5` to count the number of 5’s in a list. For example:

```count5 [] // returns 0
count5 [1;5;2;5;3;5] // returns 3```
1. Given a union data type definition of:

`type irs = I of int | R of double | S of string | IR of int * double | IS of int * string`

Define the `Imult` function that multiplies two `I` types and returns an `I` type. Ignore any other combinations.

`Imult (I 5) (I 4) // returns I 20`

1. Using the datatype definition of Q 28, define the `IxS` function that given an `I` type and a `S` type returns an `IS` type. For example:

`IxS (I 4) (S "hello") // returns IS (4, "hello")`

1. Given the following datatype definition:

`type 'e mylist = NIL | CONS of 'e * 'e mylist`

Define the function to find the length of a mylist. For example:

`myLength (CONS (8, CONS (2, CONS (6, NIL)))) // returns 3`

1. Define the recursive `rdc` function that returns all but the last element of a list.

`rdc [1;2;3] // returns [1;2]`

1. Define a tail recursive `rdc` that returns all but the last element of a list.

`tailRdc [] [1;2;3;4] // returns [3;2;1] (note: this will return the list in reverse order and that is OK)`