Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 210 Programming Languages (Spring 2022)

1.   (10 points) Please briefly describe the differences between coercion and overloading. Are they both considered as kinds of polymorphism?


2.    (10 points) For each of the following programs, give the value that ans is bound to after evaluation.

1)  val x = 1;    val y = x + 1; val x = y + 1;

val ans = x + y;


2)  val x = 1;  fun f y = x;

val x = (f 3) + (f 2);

val ans = f x;


3.   ( 15 points) Read the following ML function definition:

- fun final1 x =    =   if null x then 0

=   else hd x + final1 (tl x);

1)  What is the purpose of the function final1?

2)  What does the null x” do? Can null x” be replaced by x = []”? Why or why not?

3)   What is the function type of final1?

4)  What is the return value after calling the function shown below?

final1 [5,2,0,4,5];


4.   ( 15 points)

1)  Write a function final2 of type int list -> int list that takes a list of   integers and returns the list of all the odd elements from the original list (in the         original order). For example, if you evaluate final2 [1,2,3,4] you should get [1,3].

2)  Write a function final3 of type int list -> char list that takes a list of integers and returns the list of charaters. For example, if you evaluate final3   [65,66,67,68] you should get  [#”A”,#”B”, #”C”, #”D”].

3)  Write a function final4 of type bool list -> bool that takes a list of       boolean values and returns the logical AND of all of them. If the list is empty, your function should return true.


5.   ( 15 points) The function final5 is defined as follows:

fun final5 nil = (nil, nil)

|   final5 [a] = (nil,[a])

|   final5 (a::b::cs) =

let

val (x, y) = final5 cs

in

(a::x, b::y)

end;

val final5 = fn : 'a list -> 'a list * 'a list

Please write the returned value for each function call indicated below.

1)   final5 [4];

2)   final5 [14,24];

3)   final5 [14,15,24,25,34,35];


6.    ( 10 points) Give the ML type corresponding to each of the following sets:

1)   {true, false}

2)   {true,false} -> {true,false}

3)   {(true,false), (false,true), (true,false), (false,true)}


7.   (10 points) Consider an unknown language with a left-associative + operator that is         overloaded to have the following types: int * real -> real, int * int -> int, real * int -> real, and real * real -> real. Suppose the variable i has type int and the variable r has type real. For each + operator in each of the following           expressions, say which type of + is used:

1)   i+r

2)  r+i+r

3)   i+(r+i)

4)   i+i+r+(r+i)


8.   (15 points) What is the order of functions with each of the following ML types?

1) int * int list -> bool

2) int list * (int * int -> bool) -> int list

3) int -> int -> int-> int -> int-> bool list

4) (int -> int) * (int -> int) * (int -> int) -> int

5) int -> string