Functional programming experiment 1 / Huake

Posted by Dirtbagz89 on Fri, 07 Jan 2022 06:46:13 +0100

Task 1

Can the following pattern successfully match L of type int list? If the match is not successful, indicate the type of the pattern? (assuming x is of type int)

x::L successful

_::_ success

x::(y::L) successful

(x::y)::L failed, cannot int::int

[x, y] successful

x::L means to append x to the list L_ Wildcards can represent any type of data.:: The left must be an element and the right a list.

Task 2

Try to write the pattern corresponding to the following statement. If there is no mode corresponding to it, try to explain the reason.

list of length 3 [x,y,z]

lists of length 2 or 3 none

Non-empty lists of pairs (x,y)::L

Pairs with both components being non-empty lists
(x::L,y::L')

From pattern matching, we can know that x::L matches a list, and L is at least [], so x::L matches a list with at least one element

Task three

Analyze the following program segments (the marked line number is in the left bracket):

(1) val x : int = 3

(2) val temp : int = x + 1

(3) fun assemble (x : int, y : real) : int =

(4) let val g : real = let val x : int = 2 2

(5) val m : real = 6.2 * (real x) 12.4

(6) val x : int = 9001 9001

(7) val y : real = m * y 37.2

(8) in y – m 24.8

(9) end

(10) in

(11) x + (trunc g) 3+24

(12) end

(13)

(14) val z = assemble (x, 3.0) 27
Question: what are the types and values of the declared binding of X in line 4, m in line 5, and X in line 6? What is the result of the calculation of the expression assembly (x, 3.0) on line 14?

The answers are marked after each line. The type can be seen according to the value. ML language strictly checks the type.

Task 4

Indicate the error of the following code:

(* pi: real *)
val pi : real = 3.14159;

yes

(* fact: int -> int *)
fun fact (0 : int) : int = 1
  | fact n = n * (fact (n - 1));

yes

(* f : int -> int *)
fun f (3 : int) : int = 9
    f _ = 4;

Line 3 is missing|

(* circ : real -> real *)
fun circ (r : real) : real = 2 * pi * r   

2 is int, PI and r is real

(* semicirc : real -> real *)
fun semicirc : real = pie * r

No parameter table, no variables pie and r

(* area : real -> real *)
fun area (r : int) : real = pi * r * r

Type mismatch r:int pi:real return real

Task 5

Enter the following statements in sequence at the prompt, and observe and analyze the execution results of each statement.

3+ 4; 7

3 + 2.0; Mismatch

it + 6; 13

val it = "hello"; hello

it + “ world”; No overload

it + 5; No overload

val a = 5; 5

a = 6; false

a + 8; 13

val twice = (fn x => 2 * x);

val twice = fn : int -> int

twice a; 10

let x = 1 in x end; error

When there is no val, compare the size of x and 1

foo; there are no bindings

[1, “foo”]; Type does not match

The results of each row are placed after the corresponding row

Task 6

The function sum is used to solve the sum of all integers in the integer list. The function definition is as follows:

(* sum : int list -> int 		*)
(* REQUIRES: true		*)
(* ENSURES: sum(L) evaluates to the sum of the integers in L. *)
fun sum [ ] = 0
 |  sum (x ::L) = x + (sum L);

Complete the writing of function mult to solve the product of all integers in the integer list.

(* mult : int list -> int 		*)
(* REQUIRES: true		*)
(* ENSURES: mult(L) evaluates to the product of the integers in L. *)
fun mult [ ] = 		1	(* FILL IN *)
 |  mult (x ::L) = x*(mult L)	(* FILL IN *) 

Task 7

Write functions to achieve the following functions:

(1)zip: string list * int list -> (string * int) list

Its function is to extract the i-th element in the first string list and the i-th element in the second int list to form the i-th binary in the result list. If the length of two lists is different, the length of the result is the minimum of the length of the two parameter lists.

fun zip([]:string list,[]:int list):(string*int)list=[]
 |	zip ([]:string list,L:int list):(string*int)list=[]
 |	zip (L:string list,[]:int list):(string*int)list=[]
 |	zip (str::SL,num::NL)=(str,num)::zip(SL,NL);

(2)unzip: (string * int) list -> string list * int list

Its function is to perform the reverse operation of the zip function to decompose the elements in the binary list into two lists. The element in the first list is the list of the first element of the binary in the parameter, and the element in the second list is the list of the second element of the binary in the parameter.

- fun unzip([]:(string*int)list):((string list)*(int list))=([],[])
=  |	unzip ((str,num)::L)=let val (sl,nl)=unzip(L) in (str::sl,num::nl) end;

Is unzip (ZIP (L1, L2)) = (L1, L2) valid for all elements L1: string list and L2: int list? If so, try to prove it; Otherwise, explain the reason.

Task 8

Complete the following function mult: int list - > int, which calls mult to solve the product of all integers in int list.

(* mult : int list list -> int 	*)
(* REQUIRES: true		*)
(* ENSURES: mult(R) evaluates to the product of all the integers in the lists of R. *)
 
fun Mult [ ] = 1	(* FILL IN *)
 |  Mult (r :: R) = (mult r)*(Mult R)	(* FILL IN *)

Task 9

The function mult 'is defined as follows. Try to supplement its function description and point out the function of the function.

(* mult' : int list * int -> int 			*)
(* REQUIRES: true				*)
(* ENSURES: mult'(L, a) evaluates to the product of the integers in L and save in a (* FILL IN *) 	*)
fun mult' ([ ], a) = a
 |  mult' (x :: L, a) = mult' (L, x * a);

Use mult 'to define the function mult': int list * int - > int to make the list R and integer a of any integer list. This function is used to calculate the product of a and all integers in the list R. The function framework is as follows. Try to complete the code.

fun Mult' ( [ ], a) = a	(* FILL IN *)
 |  Mult' (r::R, a) = Mult'(R,a*(mult' (r,a)) (* FILL IN *)

Task 10

Write the recursive function square to calculate the integer square, that is, square n = n * n.

Requirement: the function double can be called in the program, but integer multiplication (*) operation cannot be used.

(* double : int -> int *)
(* REQUIRES: n >= 0 *)
(* ENSURES: double n evaluates to 2 * n.*)
fun double (0 : int) : int = 0
    | double n = 2 + double (n - 1)
fun square (0:int):int=0 
   | square n=square(n-1) +double(n)-1;

Topics: Functional Programming